0

I want to know how does one know to create and throw a checked exception or an unchecked exception.

For example I have a service which takes some data and validates it before using it. During validation a certain field did not meet the rules and I want to throw an exception say ValidationException(). How will I know of decide it should be checked or unchecked.

In another case I am calling an external web service from my code for example the google stock api. Let's assume i have a timeout of 3 seconds. If the time exprires i want to throw an exception say BackendException(). How will I know if it should be a checked exception or an unchecked exception.

Thanks in advance.

Krishna Chaitanya
  • 2,533
  • 4
  • 40
  • 74
  • Best to start with unchecked exceptions. Then, if you find a lot of programming errors coming from not handling an exception, it's time to consider maybe turning that particular exception to a checked one. – Marko Topolnik Sep 22 '16 at 14:30
  • A general rule of thumb is: if an exception is just a "failure" with no special handling except logging it and making the request fail, its should be unchecked. – Marko Topolnik Sep 22 '16 at 14:32
  • Also: [When to choose checked and unchecked exceptions](http://stackoverflow.com/questions/27578/when-to-choose-checked-and-unchecked-exceptions?rq=1) – Sotirios Delimanolis Sep 22 '16 at 14:40
  • A checked exception should be something that *might* succeed if you do exactly the same thing again (e.g. it failed because the network was down, but might be back again later). An unchecked exception should be something that won't succeed, because it's fundamentally a programming error. – Andy Turner Sep 22 '16 at 14:41
  • I find this focus on "programming error" all wrong. It's advice from the '90s. There is absolutely no prerequisite an exception must satisfy to be unchecked, it's the opposite. In practice programming errors arise _due to_ checked exceptions which force inexperienced programmers to "do something about it" where the only correct thing to do is pass it along. – Marko Topolnik Sep 22 '16 at 17:19

2 Answers2

3

There might be different opinions but I'd say the difference is how the caller should handle that exception:

  • If you want to make sure the caller handles the exception either by doing something (logging, trying to recover etc.) or rethrowing then use a checked exception. An example would be said ValidationException: if the data is invalid the caller should have to handle that, e.g. by telling someone to fix the data or by trying something else. (Note that there may be unchecked validation exceptions such as javax.validation.ValidationException. There can be various reasons for making those unchecked, e.g. if a framework doesn't allow/support checked exceptions or if some central handler will catch them anyway so the developer doesn't have to bother.)
  • It you don't want to force the caller to handle exceptions that normally should not be thrown (e.g. programming errors etc.) use an unchecked exception. An example for this might be the always dreaded NullPointerException: it should not happen that something you want to use is null so it might be considered a programming error. If something could be null but should not you might want use a checked exception instead.

    Note that some libraries/methods use IllegalArgumentException which is an unchecked exception. If this exception is thrown there's normally a programming error in that the contract of a method (e.g. the parameter value must not be negative) is violated and that the caller should fix the code or do some checks himself.

Another point of view might be: is the exception expected to be thrown in some cases or not? Expected exceptions (that still mean some kind of error happened) would be checked exceptions since that way you'd communicate to the caller that he should expect those exceptions being thrown in some cases (e.g. if the data is invalid). If the exception is unexpected you should not force the caller to handle such an exception since you'd not expect it to be thrown at all - thus it would be an unchecked exception.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • 1
    Only a note, there is an unchecked exception called javax.validation.ValidationException, so when you write about a checked "ValidationException", you should specify that you don't mean that core exception. – Gamby Jun 18 '19 at 09:14
  • @Gamby noted and added :) – Thomas Jun 18 '19 at 11:27
1

As per Joshua Bloch, Book Effective Java, Item 58 summarizing it in a single line. Thumb rule is Use checked exceptions for recoverable conditions and runtime exceptions for programming errors.

tarunkumar
  • 861
  • 2
  • 15
  • 30
  • Note that just logging an exception or printing its stack trace is not “recovering” from it. – VGR Sep 22 '16 at 14:36