Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What issues may ensue by throwing a checked exception as a RuntimeException?

I have a piece of code that encodes some kind of business data in a JSON string:

public String encodeDataAsJsonString(Data data) throws JSONException {
    JSONObject o = new JSONObject();
    o.put("SomeField1", data.getSomeProperty1());
    o.put("SomeField2", data.getSomeProperty2());
    ...
    return o;
}

The thing is:

  • JSONException is a checked exception, but
  • I don't really know how to handle it at compile time. If a JSONException really occurs, it's probably a bug in the the code and should be handled by the regular "global uncaught exception handler" which is already there (e.g. this), and which already performs all the necessary logging and cleaning up.

Thus, I ended up doing this in the calling method:

...
try {
    encoded = encodeDataAsJsonString(data);
} catch (JSONException e) {
    throw new RuntimeException(e);
}
...

It seemed like a lesser evil than adding a throws JSONException to every method up the call stack. However, it still feels dirty, hence my question:

If I want some specific checked exception to go the "regular unchecked exception route", is rethrowing it as a RuntimeException the correct idiom to use?

like image 456
Heinzi Avatar asked Oct 22 '12 17:10

Heinzi


People also ask

What's wrong with checked exceptions?

Checked exceptions leads to annoying boilerplate code ( try {} catch () {} ). Every time you call a method that throws a checked exception, you have to write the try-catch-statement. The compiler forces us to catch the exception. Often this ends up in a mixing of main logic and error handling.

Can checked exceptions occur at runtime?

Difference Between Checked and Unchecked Exceptions in JavaA checked exception is caught at compile time whereas a runtime or unchecked exception is, as it states, at runtime. A checked exception must be handled either by re-throwing or with a try catch block, whereas an unchecked isn't required to be handled.

What happens when runtime exception is thrown?

The Runtime Exception usually shows the programmer's error, rather than the condition a program is expected to deal with. Runtime Exceptions are also used when a condition that can't happen.

Why we should not throw runtime exception?

If an argument is null , the method might throw a NullPointerException , which is an unchecked exception. Generally speaking, do not throw a RuntimeException or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw.


3 Answers

The situation is quite simple: if your exception has no business value, that is, it is just a failure, definitely use an unchecked exception. If you need to handle the exception in a way specific to that exception, which in most cases means that the handling code will involve business logic, then it is still OK to use an unchecked exception, but there are at least some benefits in using a checked exception. However, in either case the raw exception you get from the JSON API is useless and it is only a sign of bad public API design.

As a side note, there's the "sneaky throw" idiom which will allow you to throw your original checked exception without wrapping:

public static <R> R sneakyThrow(Throwable t) {
  return UncheckedThrower.<RuntimeException, R>sneakyThrow0(t);
}
@SuppressWarnings("unchecked")
private static <E extends Exception, R> R sneakyThrow0(Throwable t) throws E { throw (E)t; }

Needless to say, you should be very careful about using this approach in a project.

like image 112
Marko Topolnik Avatar answered Oct 26 '22 05:10

Marko Topolnik


Short answer, yes.

You could probably create your own exception class (a child of runtime exception) and re throw that to make it easier to document, catch/ handle it where necessary. Something like hibernate does by using HibernateException, client/ calling code is not forced to catch it but can always catch it when something logical/ application specific can be done with it.

like image 38
Scorpion Avatar answered Oct 26 '22 05:10

Scorpion


Well, if you don't intend to handled the exception in you application code then you can throw it as RuntimeException.

I prefer to use com.google.common.base.Throwables to propagate this.

like image 43
Daya Avatar answered Oct 26 '22 05:10

Daya