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.
What is colour of light related to?
The resistance of a wire is 10 Ω. What will be its resistance if its length is tripled? (Assume uniform cross-sectional area)
Which of the following materials exhibits piezoelectric effect?
A liquid is kept in a regular cylindrical vessel up to a certain height. If this vessel is replaced by another cylindrical vessel having half the area o...
In order to investigate the internal atomic structure of crystals, we make use of:
The focal length of a concave mirror is 20 cm. Where should an object be placed to get an image of the same size?
Which among the following is not a characteristic of transition metals?
A copper block of mass 3 kg is heated in a furnace to a temperature of 450°C and then placed on a large ice block. Find the maximum amount of ice that...
When an electric current flows, electrons move from:
When a capacitor is fully charged in a DC circuit, the current through it is: