Forums Register Login

Checked v/s Unchecked Exception

+Pie Number of slices to send: Send

How to decide whether to make my custom exception as checked or unchecked exception in ?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Thank you all
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 828 times.
Similar Threads
NumberFormatException
Exception Doubt
Checked and Unchecked exceptions
exceptions
overriding
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:27:04.