Java | Exception Handling | Question 8
Predict the output of the following program.
class Test { int count = 0; void A() throws Exception { try { count++; try { count++; try { count++; throw new Exception(); } catch(Exception ex) { count++; throw new Exception(); } } catch(Exception ex) { count++; } } catch(Exception ex) { count++; } } void display() { System.out.println(count); } public static void main(String[] args) throws Exception { Test obj = new Test(); obj.A(); obj.display(); } } |
chevron_right
filter_none
(A) 4
(B) 5
(C) 6
(D) Compilation error
Answer: (B)
Explanation: ‘throw’ keyword is used to explicitly throw an exception.
In third try block, exception is thrown. So, control goes in catch block.
Again, in catch block exception is thrown. So, control goes in inner catch block.
Quiz of this Question
Please comment below if you find anything wrong in the above post
Recommended Posts:
- Java | Exception Handling | Question 6
- Java | Exception Handling | Question 4
- Java | Exception Handling | Question 1
- Java | Exception Handling | Question 3
- Java | Exception Handling | Question 2
- Java | Exception Handling | Question 7
- Java | Exception Handling | Question 8
- Exception Handling with Method Overriding in Java
- Nested try blocks in Exception Handling in Java
- Output of Java program | Set 12(Exception Handling)
- C++ | Exception Handling | Question 11
- C++ | Exception Handling | Question 1
- C++ | Exception Handling | Question 2
- C++ | Exception Handling | Question 3
- C++ | Exception Handling | Question 4



