Question
Which of the following correctly handles multiple
exceptions in Java?Solution
In Java, a single try block can handle multiple exceptions by attaching multiple catch blocks. Each catch block specifies a distinct exception type. The exception hierarchy determines the order of catch blocks—child exceptions must precede parent exceptions. Example: try {   int a = 5 / 0;  // ArithmeticException   int[] arr = new int[2];   System.out.println(arr[3]); // ArrayIndexOutOfBoundsException } catch (ArithmeticException e) {   System.out.println("Arithmetic Exception caught"); } catch (ArrayIndexOutOfBoundsException e) {   System.out.println("Array Index Out of Bounds Exception caught"); } catch (Exception e) {   System.out.println("Generic Exception caught"); } ________________________________________ Why Other Options Are Incorrect: 1. Separate try blocks for each exception: This is inefficient and makes code less readable. A single try block with multiple catch blocks is the recommended approach. 2. Nesting try blocks within each other: This is not a standard practice unless the logic explicitly demands it. It can lead to convoluted and hard-to-maintain code. 3. Single catch block for all exceptions: While possible, this is a poor practice as it does not differentiate exception types, leading to less precise error handling. 4. Writing the exception type as a string: Java’s catch block requires exception classes, not strings, for type checking.
My sister is 5 years elder to me. My father was 30 years of age when my brother was born while my mother was 28 years of age when I was born. If my bro...
Rishav's current age is 25% more than Sourav's current age. In 8 years, the ratio of their ages will be 5:6. What will be Rishav's age 10 years from now?
The average age of A, B and C is 40 years, while the average age of B, A and D is 30 years. If the ratio of the age of C and D is 5:3, respectively, the...
A girl’s age is 125% of what it was 10 years ago, but 75% of what it will be after 10 years. What is her present age?
Four years from now, the age of 'A' will be 60% of the age of 'B' at that time. The combined present ages of 'A' and 'B' exceed the current age of 'C' b...
When 15 years ago age of Dev was divided by 20, the present age of his granddaughter Swati is obtained. If Swati is 6 years younger than her sister Neha...
Aarav's current age is 6 years less than the total of 30% of Neel’s age and 50% of Tarun’s age. If Neel is 70 years old and Tarun is 40 years old, h...
The age of A is 20% less than that of B. The age of B is 25 years more than the average age of his two friends whose total age is 30 years. Find the dif...
Sum of the present ages of A, B, C and D is 56 years. After 3 years ratio of their ages is 7:6:5:2. What is C’s present age?
The ratio of age of ‘B’ after 7 years from now and age of ‘C’ 4 years ago from now is 7:4, respectively. The present age of ‘C’ is 30% of th...