aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception doubt" Watch "Exception doubt" New topic
Author

Exception doubt

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi All,
The output for the below code is BAFF.Can anybody explain me how?

class ExceptionA extends Exception {}
class ExceptionB extends ExceptionA {}

public class ObjectTest {

public static void main(String[] args) {
try {
throw new ExceptionB();
} catch (ExceptionB b) {
System.out.print("B");
try {
throw new ExceptionB();
} catch (ExceptionA e) {
System.out.print("A");
} finally {
System.out.print("F");
}
} catch (ExceptionA a) {
System.out.print("A");
} finally {
System.out.print("F");
}
}
}
Source:TeamTesting

Thanks All
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
First of all no problem with code, compiles and runs fine.



Where was your doubt exactly?

Thanks,


cmbhatt
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367


An ExceptionB object is thrown in line 8 and is caught by the catch block in line 9 causing B to be printed.

An ExceptionB object is thrown in line 13, and since ExceptionB is a subclass of ExceptionA, it is caught by the catch block in line 14, causing an A to be printed.

Since a finally block is always executed unless the JVM shuts down, an F is printed.

Then the finally block associated with the first try is executed causing another F to be printed.
[ May 08, 2007: Message edited by: Keith Lynn ]
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
THANKS K
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Thanks keith
 
jQuery in Action, 2nd edition
 
subject: Exception doubt