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 throwsIllegalArgumentExceptionwith message"Inner error"middleMethod(): CallsinnerMethod(), catches the exception, prints"Caught in middle", then throws a newRuntimeExceptionwith message"Middle: " + original messageouterMethod(): CallsmiddleMethod(), catches the exception, prints"Final handler: " + exception messagemain(): CallsouterMethod()
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