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
- Define
parseElement(String[] data, int index)method - Convert the element at the given index using
Integer.parseIntand return it - When the string cannot be parsed,
NumberFormatExceptionpropagates - When the index is out of bounds,
ArrayIndexOutOfBoundsExceptionpropagates
Example Behavior
parseElement(new String[]{"10", "20", "30"}, 0)→10parseElement(new String[]{"10", "abc", "30"}, 1)→NumberFormatExceptionparseElement(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