019-004-011

Exception Propagation with throws

Easy

Problem Description

Exception Propagation with throws

In this problem: You will create a program that uses the throws declaration on a method to propagate an exception to the caller, where it is caught with try-catch.

Learning Objective: Understand how checked exceptions propagate through the throws declaration

Overview

Java checked exceptions must either be handled within the method or propagated to the caller using the throws declaration. In this problem, a method simulating file reading throws an exception, and the calling main method catches it.

Specifications

  • readConfig(String filename) method: with throws Exception declaration
  • If filename is "error.txt", throw an Exception
  • Otherwise return a string simulating file content
  • Use try-catch in main to print result or error message

Output Format

Config: settings loaded
Error: Config file not found: error.txt

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up