Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

should user defined exception be a checked or unchecked exception?

 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • 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
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul for your reply. so checked exception is the right choice.
 
Stephan van Hulst
Bartender
Pie
Posts: 5415
52
  • Mark post as helpful
  • send pies
  • 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.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic