019-003-010

Multi-Catch Basics

Easy

Problem Description

Multi-Catch Basics

In this problem: You will use the multi-catch syntax catch(A | B e) introduced in Java 7 to handle multiple exceptions in a single catch block.

Learning Objective: Understand the syntax and benefits of multi-catch

Overview

Multi-catch allows different exception types to be handled in a single catch block, eliminating duplicate handling code across multiple catch blocks.

Specifications

  • Parser class:
    • parse(String input): converts input string to integer
      • If null: throws NullPointerException
      • If non-numeric string: NumberFormatException occurs
      • If valid: returns the converted number
  • In Main, test with "42", null, "abc"
  • Use multi-catch for NullPointerException | NumberFormatException

Output Format

Parsed: 42
Error: Cannot parse null input
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