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 boat covers 90 km downstream and 70 km upstream in a total time of 10 hours. How much time will it take to travel 126 km downstream and 98 km upstream?
Ratio of speed of boat in downstream and speed of stream is 7:5, if speed of current is 5 km/hr, then find distance travelled (in km) upstream in 4 hours.
A boat can travel 7.2 km upstream in 18 min. If the ratio of the speed of the boat in still water to the speed of the stream is 7:1. How much time will ...
A man can row at 30 km/hr in still water. If the velocity of current is 6 km/hr and it takes him 1 hour to row to a place and come back, how far is the ...
The speed of a boat in still water is twice the speed of the current. If the boat takes 10 hours to cover 150 km upstream, then find the time taken by t...
A motor boat goes downstream from point A to B ,which is 36 km away from point A, and then returns to A. If actual speed of the boat in still water is 7...
The speed of a boat in still water is 12 times the speed of the stream. If the difference between the upstream and downstream speed of the boats is 20 k...
A boat travels 48 km downstream in 4 hours and 24 km upstream in 6 hours. What is the speed of the boat in still water?
A boy can swim in water with a speed of 12 km/hr in still water. If the speed of the stream is 6 km/hr, what will be the time taken by the boy to cover ...
The ratio of the speed of the boat in upstream to that of in downstream is 7:10, respectively. If the speed of the current is 12 km/h, then find the spe...