Why is catching throwable bad




















In fact, even catching Exception is usually a bad idea. Let's consider an example:. Now, let's say that getUserInput blocks for a while, and another thread stops your thread in the worst possible way it calls thread. Your catch block will catch a ThreadDeath Error. This is super bad. The behavior of your code after catching that Exception is largely undefined. A similar problem occurs with catching Exception. Connect and share knowledge within a single location that is structured and easy to search.

Besides, Throwable covers Error as well and that's usually no point of return. This is a bad idea. In fact, even catching Exception is usually a bad idea.

Let's consider an example:. Now, let's say that getUserInput blocks for a while, and another thread stops your thread in the worst possible way it calls thread. Your catch block will catch a ThreadDeath Error. This is super bad. The behavior of your code after catching that Exception is largely undefined. A similar problem occurs with catching Exception.

Maybe getUserInput failed because of an InterruptException, or a permission denied exception while trying to log the results, or all sorts of other failures. You have no idea what went wrong, as because of that, you also have no idea how to fix the problem. Also be aware that when you catch Throwable , you can also catch InterruptedException which requires a special treatment. See Dealing with InterruptedException for more details.

This way, when you modify your code and add a method call that can throw a checked exception, the compiler will remind you of that and then you can decide what to do for this case. It's a bad practice if you really can't handle the exception. Better to add "throws" to the method signature than just catch and re-throw or, worse, wrap it in a RuntimeException and re-throw. Catching Throwable is sometimes necessary if you are using libraries that throw Errors over-enthusiastically, otherwise your library may kill your application.

However, it would be best under these circumstances to specify only the specific errors thrown by the library, rather than all Throwables. Throwable is the base class for all classes than can be thrown not only exceptions. The question is a bit vague; are you asking "is it OK to catch Throwable ", or "is it OK to catch a Throwable and not do anything"? If you propagate the exception, the answer like the answer to so many questions is "it depends".

It depends on what you're doing with the exception—why you're catching it. A good example of why you would want to catch Throwable is to provide some sort of cleanup if there is any error. For example in JDBC, if an error occurs during a transaction, you would want to roll back the transaction:.

But as a general policy, catching Throwable because you don't have a reason and are too lazy to see which specific exceptions are being thrown is poor form and a bad idea. If there is any specific exception that you can possibly react on in a meaningful way, you could catch it first and do so.

If there isn't and you're sure you will do the same thing for all exceptions and errors for example exit with an error-message , than it is not problem to catch the throwable. Usually the first case holds and you wouldn't catch the throwable.

But there still are plenty of cases where catching it works fine. Although it is described as a very bad practice, you may sometimes find rare cases that it not only useful but also mandatory. Here are two examples. In a web application where you must show a meaning full error page to user.

As another example, consider you have a service class which serves fund transfer business. Now imaging you get a List of fund transfers from user and you must use above service to do them all. But what will happen if any exception happens? You should not stop, as one transfer may have been success and one may not, you should keep go on through all user List , and show the result to each transfer. So you end up with this code. You can browse lots of open source projects to see that the throwable is really cached and handled.

For example here is a search of tomcat , struts2 and primefaces :. Generally speaking you want to avoid catching Error s but I can think of at least two specific cases where it's appropriate to do so:. Throwable is the superclass of all the errors and excetions. If you use Throwable in a catch clause, it will not only catch all exceptions, it will also catch all errors. Errors are thrown by the JVM to indicate serious problems that are not intended to be handled by an application.

I generally don't really take into consideration the "performance" side of things, at least on the server :. In your case, yes, you should just catch those exceptions and do something helpful probably not just eat them--you could throw after you log them. This is pretty much equivalent to what your code does.

Your dev probably did it that way to avoid duplicating the "log error and eat it" blocks. If you check the code for Platform. I'd say that it is a really bad idea. A lot of code is implemented on the assumption that if you catch Error and Exception you have caught all possible exceptions.

And most tutorials and textbooks will tell you the same thing. By creating a direct subclass of Throwable you are potentially creating all sorts of maintenance and interoperability problems. I can think of no good reason to extend Throwable.

Extend Exception or RuntimeException instead. Exceptions are a very expensive way of dealing with "normal" flow control. In some cases, we are talking thousands of extra instructions executed to create, throw and catch an exception.



0コメント

  • 1000 / 1000