• 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

correct order of declaring Exceptions.

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello to all..

I got a doubt..in which order ..we need declare exeptions..in the catch block ..first checked, or unchecked excpetion.

please tell me the order for this..

SQLException ,NumberFormatException,NullPointerException.

Thanks
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Java In General (Beginner)
This is not a servlet question.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The order may be
NullPointerException, NumberFormatException,SQLException,....,Exception.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether an exception is checked or unchecked doesn't affect the ordering. Instead what matters is inheritance. If you need to catch two exception types, and one is a subtype of the other -- for example, FileNotFoundException and IOException (the first is a subclass of the second) then you need to put the subclass first, or the other catch block would catch it. The compiler will generally refuse to compile the code if you do it the wrong way, as then the later catch blocks would be unreachable.
 
aman hindustani
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks mr Eanest.......for u reply....
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ones to be extra-careful of are Exception and Throwable.

There are not very many cases where it is valid to catch Exception (a lot of the time, people are just being lazy and not listing the true set of exceptions). There are only a very few cases where it is valid to catch Throwable.

However, if you do decide to catch Exception or Throwable, be aware that you will catch things that would normally sail straight on through, such as RuntimeException (and all subclasses) and Error (and all subclasses). That is often not what you want.

I've often thought it is a shame that Java checked exceptions descend from the same superclass (Exception) as RuntimeException. Does anyone know why they didn't make Exception abstract and then introduce a CheckedException subclass, from which all checked exceptions would descend?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic