• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

should user defined exception be a checked or unchecked exception?

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If i need to define a user-defined ApplicationException in my web application.Should i go for Checked or Unchecked exception?What are there advantages and disadvantages ?

Thanks,
Rajvinder
 
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a link to the Java Exceptions tutorial (Google keywords: java exception tutorial). You'll notice that it has a whole section which specifically addresses your question.
 
raj malhotra
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul for your reply. so checked exception is the right choice.
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. It depends on your situation.

Indeed, often user defined exceptions are checked exceptions, because already so many suitable unchecked exceptions exist for simple programming errors.

However, it's not completely unthinkable that a client uses your code in a bad manner, and this exceptional situation is not fully covered by the standard unchecked exceptions provided by the Java API.
Or maybe the standard unchecked exceptions are too broad, and you need a more specific exception.

The only thing you should consider when you define an exception is: "Should I allow the user to recover from this exception?". If yes, define a checked exception. If no, use an unchecked exception.
 
Master Rancher
Posts: 5161
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, note that there are many people (inside and outside of the current Java community) who think checked exceptions were a bad idea. There are a number of other languages developed after Java that took many good ideas from Java (just as Java took many good ideas from its predecessors) but I don't know any other language that uses checked exceptions. (Does anyone else know of one?) Even within Java, there are popular tools and frameworks that make almost everything into a runtime exception. Hibernate and Spring, for example.

So, while the tutorial will tell you Sun's official position on checked exceptions (which may or may not be Oracle's position, but so far they haven't signaled any change)... be aware that you may well find some co-workers who think you should not use checked exceptions at all if you can avoid it. There is no single answer to this - it's a controversial area.
reply
    Bookmark Topic Watch Topic
  • New Topic