019-004-013

Catching and Rethrowing Exceptions

Medium

Problem Description

Catching and Rethrowing Exceptions

In this problem: You will create a program that catches an exception, logs it, and then rethrows it to propagate to the caller.

Learning Objective: Understand the catch-and-rethrow pattern using throw inside a catch block

Overview

In exception handling, a common pattern is for intermediate methods to catch an exception, log it, and rethrow it to delegate final handling to a higher-level caller. This allows each layer to perform its own processing while leaving the ultimate exception handling to the caller.

Specifications

  • parseNumber(String input) method: converts string to integer, throws NumberFormatException on failure
  • processInput(String input) method: calls parseNumber, logs on catch and rethrows
  • main catches the final exception and prints an error message

Output Format

Result: 42
[LOG] Parse error in processInput: For input string: "abc"
Final error: For input string: "abc"

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