Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Exception  RSS feed

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, why don't let the compiler correct you if you're wrong?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, why not try the code? You just need to add a few things to get it to run:

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!