019-002-008

Catching Multiple Exceptions and Resource Management

Medium

Problem Description

Exception Propagation and Resource Management

In this problem, you will implement a parseElement method that converts string array elements to integers. The method should allow NumberFormatException and ArrayIndexOutOfBoundsException to propagate naturally.

Learning Objective: Understand how exceptions propagate in method implementations and verify that the correct exception is thrown for each input condition.

Overview

Implement a method that converts the string element at a given index to an integer. When conversion fails or the index is out of bounds, let the exception thrown by Java propagate naturally.

Specifications

  1. Define parseElement(String[] data, int index) method
  2. Convert the element at the given index using Integer.parseInt and return it
  3. When the string cannot be parsed, NumberFormatException propagates
  4. When the index is out of bounds, ArrayIndexOutOfBoundsException propagates

Example Behavior

  • parseElement(new String[]{"10", "20", "30"}, 0)10
  • parseElement(new String[]{"10", "abc", "30"}, 1)NumberFormatException
  • parseElement(new String[]{"10", "20"}, 5)ArrayIndexOutOfBoundsException

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