Java Exception Handling Best Practices

I have some questions regarding exception handling in Java. I read a little about it and got some conflicting recommendations.

Exception Handling Considerations

Go to the specified article:

It says that you should generally avoid using checked exceptions if "Customer Code cannot do anything." But what does that mean? Is the error message in the GUI sufficient reason to spam the checked exception? But it made the GUI programmer remember to catch RuntimeExceptions and their descendants in order to display potential error information.

The second view presented in this article is that you should abandon the invention of your own exception classes if I do not want to introduce any customs field / methods into them. I don’t agree with this at all, my practice today was just the opposite: I wrapped the exceptions in my own exception structure to reflect the goals realized by the classes that I write, even if they simply extend Exception without adding any new methods. I think this helps to cope with them more flexibly in the higher layers when throwing, and also more generally understandable and understandable for the programmer who will use these classes.

I implemented some code today in a “new way”, presented in an article throwing a RuntimeException here and there, then I let Sonar parse it. To confuse me, even more Sonar marked my RuntimeExceptions as the main errors with a message like "Avoid throwing root type exceptions, wrap in your own types."

So it looks rather controversial, what do you think?

I also heard today from one of the technologists that just throwing an exception is a bad thing because it is a very expensive operation for the JVM. For me, on the other hand, throwing SQLExceptions or IOExceptions around the world seems a bit intermittent encapsulation.

So what is your general attitude to the questions that I have presented here?

  • When to transfer exceptions from my own types, when should I not?

  • Where is this point 'the client can do nothing, throw a runtime exception? '

  • What about performance issues?

+7
source share
1 answer

It seems that your technical leader, as often, escaped his role as a developer because he was poorly versed in this.

My tips:

  • prefer runtime exceptions to checked exceptions, especially if you are not the only client of your API. Using a checked exception forces each client to handle the exception, even if they cannot do anything about it. If this is really what you want to do (i.e., forcing the caller to process it), then the checked exception is what you want.
  • if the only thing the client can do when an exception occurs is to display a more or less general error message, such as "oops, something is bad, try again or return to the welcome page" and then be sure to use exceptions at runtime . Most presentation frameworks provide a way to use a common error handler.
  • Definitely use exceptions related to your level of abstraction. Throwing a SQLException from a high-level service is not enough. Use existing exception types when appropriate (for example, IllegalArgumentException to signal an illegal argument). Otherwise, wrap the low level exception in a higher level that matches the type of exception. Extremely expensive. Whether it is wrapping another or not does not really matter. And this should happen only in any case.
+2
source

All Articles