• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checked v/s Unchecked Exception

 
Amruta Jegarkal
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How to decide whether to make my custom exception as checked or unchecked exception in ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15279
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amruta Jegarkal
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic