File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes should user defined exception be  a checked or unchecked exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "should user defined exception be  a checked or unchecked exception?" Watch "should user defined exception be  a checked or unchecked exception?" New topic
Author

should user defined exception be a checked or unchecked exception?

raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
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
Bartender

Joined: Oct 14, 2005
Posts: 18879
    
    8

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

Joined: Feb 22, 2007
Posts: 285
Thanks Paul for your reply. so checked exception is the right choice.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: should user defined exception be a checked or unchecked exception?