• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception handling  RSS feed

 
Shashankshanky Gupta
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an exception class:


Then I have a main function :


Problem : my main function is not catching daoException when NmberFormatException occurs though daoException extends Exception

Please help me
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..thats's because NumberFormatException is not daoException, and your catch block expects daoException while Integer.parseInt
throws NumberFormatException.

Manish
 
Shashankshanky Gupta
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then how can thow daoException even when a numberFormatException occurs?
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...just catch NumberFormatException rather than daoexception, and inside catch you can throw your customised exception.

Manish
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer.parseInt(...) might throw a NumberFormatException, not a daoException. Just creating a subclass of NumberFormatException will not make it suddenly throw a daoException.

Note that daoException is a subclass of NumberFormatException - not the other way around. When you have a catch that catches exceptions of type X, then the catch block will run whenever an exception of type X or a subclass of X occurs within the try block.

So, if you catch daoException, it will catch daoException and any exceptions that are instances of subclasses of daoException. Note that NumberFormatException is not a subclass, but a superclass of daoException. Therefore, the catch block is not going to catch NumberFormatException, if you specify it to catch daoException.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!