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.
Among the following insect orders, which is exclusively herbivorous in nature?
Which of the following is not one of the advantages of PM KISAN scheme?
How many houses have been completed under the Pradhan Mantri Awas Yojana (PMAY) in the last 10 years?
The Union Budget proposes to develop how many weekly 'haats' or street food hubs in select cities over the next five years?
Recently the Central Government has given approval for creation of Bulk Drug Parks in which of the following places?
I- Himachal Pradesh
...What does the acronym ULLAS stand for in the context of the ULLAS- Nav Bharat Saaksharta Karyakram scheme?
What is the primary objective of the 'Prime Minister's Scholarship Scheme (PMSS)'?Â
According to the new rules (made in Sept’22) of Companies (Corporate Social Responsibility Policy) Amendment Rules, 2022, the expenditure for social i...
Which of the following is most likely to cause a rightward shift in the demand curve for laptops?
When was the Employees' Provident Funds and Miscellaneous Provisions Act enacted?