What problems can occur if you select an exception as a RuntimeException?

I have a piece of code that encodes some 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 fact is that

  • JSONException is a checked exception, but
  • I do not know how to handle it at compile time. If a JSONException does occur, it is probably a bug in the code and should be handled by the usual "global uncaught exception handler" that already exists (for example, this ) and which already does all the necessary registration and cleaning.

So I did this in the calling method:

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

This seemed less evil than adding a throws JSONException for each method to the call stack. However, it still feels dirty, so my question is:

If I want a specific exception to be thrown into the "regular route of an unchecked exception", reconstruct it as a RuntimeException for the correct idiom to use?

+4
source share
6 answers

The short answer is yes.

You could probably create your own exception class (a child element of the exception at runtime) and repeat it to simplify the documentation, catch / process it where necessary. Something like hibernate, using a HibernateException, the client / calling code will not make it catch, but it can always catch it when something logical / application can be done with it.

+4
source

The situation is quite simple: if your exception does not have business value, that is, it is just a failure, of course, use the exception without checking. If you need to handle the exception in a way specific to this exception, which in most cases means that the processing code will include business logic, then it is still OK to use the excluded exception , but there are at least some advantages when using the checked exception. However, in any case, the unhandled exception that you get from the JSON API is useless, and this is just a sign of poor public API design.

As a side note, I came across this beautifully stupid piece of code (author: Lukas Eder) that will allow you to throw your original excluded exception without packaging!

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

Of course, these are powerful things that should only be consumed by adult Compromisers.

+4
source

Your Java code has an argument for using only the thrown exception. If you want to follow this approach, wrap the checked exception, since you made it the only reasonable thing. FWIW, I have used this approach many times, and it has been helpful.

Even if you do not want to buy in this style, it may be useful to convert some checked exceptions to runtime exceptions.

+1
source

I do this all the time. Yes, this is a good approach. No, this is not a dirty approach. Personally, I cannot check for exceptions. I transfer all checked exceptions as runtime exceptions regardless of the type of exception.

+1
source

Well, if you do not intend to handle the exception in your application code, you can send it as a RuntimeException .

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

+1
source

A checked exception is a way of communication between developers. The checked exceptions say, "rule me." If you know what you are doing, you can cancel the exceptions (and the log).

EDIT: as in the other case, rewrap exception is also good advice.

0
source

Source: https://habr.com/ru/post/1441344/


All Articles