Java | Exception Handling | Question 7
Predict the output of the following program.
class Test { String str = "a"; void A() { try { str +="b"; B(); } catch (Exception e) { str += "c"; } } void B() throws Exception { try { str += "d"; C(); } catch(Exception e) { throw new Exception(); } finally { str += "e"; } str += "f"; } void C() throws Exception { throw new Exception(); } void display() { System.out.println(str); } public static void main(String[] args) { Test object = new Test(); object.A(); object.display(); } } |
chevron_right
filter_none
(A) abdef
(B) abdec
(C) abdefc
Answer: (B)
Explanation: ‘throw’ keyword is used to explicitly throw an exception.
finally block is always executed even when an exception occurs.
Call to method C() throws an exception. Thus, control goes in catch block of method B() which again throws an exception. So, control goes in catch block of method A().
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 8
- 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



