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?
Michał Wróbel
source share