• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, why not try the code? You just need to add a few things to get it to run:

 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic