• 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 question

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic