aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception 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" Watch "Exception" New topic
Author

Exception

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Here is a question from one of the mock exam:
Q. Consider the code below:
void myMethod()
{
try
{
fragile();
}
catch( NullPointerException npex )
{
System.out.println(
"NullPointerException thrown " );
}
catch( Exception ex )
{
System.out.println( "Exception
thrown " );
}
finally
{
System.out.println( "Done with
exceptions " );
}
System.out.println(
"myMethod is done" );
}
What is printed to standard output if fragile() throws an IllegalArgumentException?
a) "NullPointerException thrown"
b) "Exception thrown"
c) "Done with exceptions"
d) "myMethod is done"
e) Nothing is printed
The answers are b,c and d.
My answer was only (c), since the IlllegalArgumentException is not handled in the code, none of the catch statements is executed and only the finally clause executes and prints the statement "Done with exceptions" and terminates abnormally with a runtime exception. Am I correct here? Can anyone please clarify this?
Thanks..

[This message has been edited by Java2learner (edited February 29, 2000).]
[This message has been edited by Java2learner (edited February 29, 2000).]
Rolf Weasel
Ranch Hand

Joined: Feb 26, 2000
Posts: 82
The statement "catch (ExceptionType)" will catch all exceptions of the type ExceptionType and all exceptions that can be CAST to ExceptionType. Hence, since Exception is the super class of all exceptions, IllegalArgumentException, and in fact all other exceptions, can be caught by the statement catch(Exception).
Note that the Runtime Environment steps through the defined catch's in the order that they are written in the code executes the first acceptable catch. hence, in your code, after all the catch's already present, if you were to add another one for IllegalArgumentException, that handler would never be executed even of an IllegalArgumentException were thrown since, when the Runtime reaches catch(Exception), it would run that handler and consider the IllegalArgumentException as handled.


May the Moose be with you.
Greg Torrance
Greenhorn

Joined: Feb 25, 2000
Posts: 17
I believe it would also be a compile-time error to place a catch for IllegalArgumentException after a catch for Exception, because as you said, the catch(IllegalArgumentException) would never have an opportunity to be executed due to being "shadowed" by Exception. [Correct me if I'm wrong.]
Greg
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Well, why don't let the compiler correct you if you're wrong?


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Ramana Namuduri
Greenhorn

Joined: Mar 02, 2000
Posts: 16
I think answer d is not correct because once an exception is caught, the corresponding catch statement is executed and finally block is also executed. After that block, compiler wont execute any other instructions because an exception is already caught.
Rgds
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, why not try the code? You just need to add a few things to get it to run:


"I'm not back." - Bill Harding, Twister
 
jQuery in Action, 2nd edition
 
subject: Exception