019-004-010

Exception Propagation Chain: Exception Flow Between Methods

Medium

Problem Description

Exception Propagation Chain: Exception Flow Between Methods

In this problem: You will implement three methods innerMethod(), middleMethod(), and outerMethod() to create a program that demonstrates how exceptions propagate and are rethrown through a method chain, with output to standard output.

Learning Objective: Understand exception propagation and rethrowing after catching

Overview

Java exceptions propagate up through the method call hierarchy. By catching an exception at an intermediate level, adding context, and rethrowing it, you can build up error context as the exception travels up the call stack.

Specifications

  • innerMethod(): Directly throws IllegalArgumentException with message "Inner error"
  • middleMethod(): Calls innerMethod(), catches the exception, prints "Caught in middle", then throws a new RuntimeException with message "Middle: " + original message
  • outerMethod(): Calls middleMethod(), catches the exception, prints "Final handler: " + exception message
  • main(): Calls outerMethod()

Output Format

Caught in middle
Final handler: Middle: Inner error

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