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

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 136
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...just catch NumberFormatException rather than daoexception, and inside catch you can throw your customised exception.

Manish
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic