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 the main objective of the AI-powered platform 'Incubators' developed by the Centre for Research on Start-ups and Risk Financing (CREST) at IIT M...
IMF reached a staff-level agreement with Pakistan for the final review of a $3 billion bailout. What is the amount of the last tranche pending release u...
Who is the author of the book ‘Dashkumaracharitam’?
Which ministers jointly launched the National Initiative on Water Security?
Who is the recipient of the 'Polly Umrigar' award at the BCCI Awards 2024, recognizing performances during the 2022-23 season, held in Hyderabad?
Which of the following statements about the Global Tourism Index 2024 is/are incorrect?
1. India moved up to the 39th spot on the Travel & Touris...
After a prolonged period of hot and dry weather, some parts have been given Drought status by which country recently?
- Who became the seventh Indian cricketer to play 300 ODIs?
Nation First Transit Card has been launched to provide seamless and convenient customer commuting experience and ensure easy digital ticketing fare paym...
What is the primary objective of the Indian Military Heritage Festival inaugurated by Raksha Mantri Rajnath Singh?