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.
Power of SEBI to issue directions under SEBI Act shall
What are the conditions required to be fulfilled to be evidence that an allotment of, or an agreement to allot, securities was made with a view to the s...
The procedural regulation power under Section 9(2) requires:
Section 8 prohibits handling hazardous substances except in compliance with procedural safeguards. Which of the following matches the statutory intent?�...
Which of the following is not a “Document ” as per Indian Evidence Act, 1872?
Which of the following companies are required to appoint Internal Auditor to conduct internal audit of the functions and activities of the company?
Under the PMLA, a person is guilty of money laundering if they are found to have directly or indirectly engaged in which of the following activities con...
Under the Right to Information (RTI) Act, 2005, information cannot be denied if:
According to Section 56 of the Indian Contract Act, which of the following statements accurately reflects the treatment of agreements to perform impossi...
A Public Company is a company which has a minimum paid-up share capital ____________