my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Checked v/s Unchecked Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Checked v/s Unchecked Exception" Watch "Checked v/s Unchecked Exception" New topic
Author

Checked v/s Unchecked Exception

Amruta Jegarkal
Greenhorn

Joined: Nov 29, 2012
Posts: 29

How to decide whether to make my custom exception as checked or unchecked exception in ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Welcome to the Ranch!

The page The Catch or Specify Requirement from Oracle's Java Tutorials explains the three kinds of exceptions, and when each type is appropriate. There is, however, some debate about this, and there isn't always a clear answer to the question whether a certain exception should be a checked or an unchecked exception.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Amruta Jegarkal wrote:How to decide whether to make my custom exception as checked or unchecked exception in ?

There are many criteria, but the biggest one usually is: Can the program recover from the error?

If it can, then the usual rule of thumb is to make the Exception checked, which tends to mandate the use of try...catch blocks (unless you're happy to have throws specifications all over the place).

If it can't, then your program will need to abort, so the usual thing is to make it a subclass of RuntimeException (ie, unchecked).

Winston

PS: However, take a good look at the link that Jesper provided, because it will explain things a lot better than I did.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1770
    
    7

Jesper de Jong wrote:Welcome to the Ranch!

The page The Catch or Specify Requirement from Oracle's Java Tutorials explains the three kinds of exceptions, and when each type is appropriate. There is, however, some debate about this, and there isn't always a clear answer to the question whether a certain exception should be a checked or an unchecked exception.


Right. If you're interested this article is a worthwhile read.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Amruta Jegarkal
Greenhorn

Joined: Nov 29, 2012
Posts: 29
Thank you all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checked v/s Unchecked Exception