This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Exception question  RSS feed

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If there was no catch for RuntimeException and only a catch for
Exception, would it still create a compiler error
i.e, If there is no child exception would the parent
exception take care of it's children's exception.
If the parent and child exception catch blocks are present,
if case of a try block is calling the child exception,
would the child exception and parent exception's catch blocks
be called in the order,
i)child exception catch block and ii)parent exception catch block
Thanks.
-Jay
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jay Kay:

If there was no catch for RuntimeException and only a catch for
Exception, would it still create a compiler error
i.e, If there is no child exception would the parent
exception take care of it's children's exception.
If the parent and child exception catch blocks are present,
if case of a try block is calling the child exception,
would the child exception and parent exception's catch blocks
be called in the order,
i)child exception catch block and ii)parent exception catch block
Thanks.
-Jay


Parent will take care if the child exception is thrown
If both parent and child are present in a try/catch mechanism
for ex: try ... catch(child) catch (parent) (this is the legal order also...)
Then the child catch alone will be invoked when a child is thrown
Always think Parent to be a last resort
Ragu
 
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jay Kay:

If there was no catch for RuntimeException and only a catch for
Exception, would it still create a compiler error
i.e, If there is no child exception would the parent
exception take care of it's children's exception.


A catch block declared to catch Exception will catch RuntimeExceptions too, since the latter is a subclass of the former.
Yes the parent exception takes care of a child exception, in clear an Exception declared in a catch block will catch all Exception downwards its hierarchy (i.e. all its subclasses)

Originally posted by Jay Kay:

If the parent and child exception catch blocks are present,
if case of a try block is calling the child exception,
would the child exception and parent exception's catch blocks
be called in the order,
i)child exception catch block and ii)parent exception catch block


If the parent and child exception are present the child must come first otherwise there will be a compiler error (catch not reached). Remeber that there is only ONE catch block that will be executed and not every catch block matching the exception thrown !!! This is very important so there is no such thing as the catch block being called in order when child and parent exceptions are thrown...
HIH


------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Jay Kay
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, should the try, catch always be in this order
try { }
catch(child)
catch (parent)
(only one catch is caught)
finally()
(finally is always caught)

Thanks Raghu and Valentin for your help.
-Jay
 
Valentin Crettaz
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jay Kay:
So, should the try, catch always be in this order
try { }
catch(child)
catch (parent)
(only one catch is caught)
finally()
(finally is always caught)

Thanks Raghu and Valentin for your help.
-Jay


That's right although catch and finally blocks are not "caught" but executed, Throwables and subclasses are caught, but your statements are correct

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited October 04, 2001).]
 
Not so fast naughty spawn! I want you to know about
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!