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.
A, B, and C started a partnership with initial investments of Rs. 17,200, Rs. 25,800, and Rs. 32,250, respectively. After 20 months, C completely withdr...
In a business there are two partners ‘P’ and ‘Q’. ‘P’ started the business with the capital of Rs. 20000 and after 3 months ‘Q’ joined h...
A store owner sold a product at a profit of 22%. Had he sold it at a profit of 15%, he would have gained Rs.140 less. Determine the cost price.
‘P’ and ‘Q’ entered into a business with initial investments of Rs. 1000 and Rs. 700 respectively. After 3 months, ‘P’ withdrew Rs. 300 whil...
Kamal invested Rs. T and Rohan invested Rs. (T + 30) for 27 months and 18 months respectively. Their profit ratio is 9 : 8. Find Rohan’s investment.
Makhan Lal started a business with the capital investing Rs 18,400. After 6 months Arvind Lal invested Rs 16200. At the end of one year they made a prof...
Raj invested Rs.35000 in a business. After 6 months, Rohan joins him with an investment of Rs.P. If at the end of the year the profit is Rs.60000 and pr...
The savings of E and F are identical. The difference between F's expenditure and the combined savings of E and F is zero. B's income is Rs. 36,000, and ...
A and B together started a business with initial investment in the ratio of 4:5, respectively. The time-period of investment for A and B is in th...
'G' and 'H' entered into a partnership with 'G' investing 30% more than 'H'. After 10 months, 'H' took out his investment. If the difference in their pr...