Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is catch block able to catch Throwable (both error and exception)

Tags:

java

exception

In one of my interview, they asked me, is it possible to write Throwable in catch() like this

try{
some code
}
catch(Throwable t)
{
}

I said yes. it will not give a compile time error but jvm would not handle it if there occur an Error(sub class of Throwable ), since Errors are irreversible conditions that can not be handled by jvm. than they further asked than what is the use of writing Throwable .

Please give me proper reply can we use Throwable in catch. if yes why.

like image 874
Romi Avatar asked Jul 21 '11 06:07

Romi


People also ask

Can we catch and throw the same exception in Java?

We can perform such activities in the catch block and re-throw the exception again. In this way, a higher level gets notified that the exception has occurred in the system.

Does catch exception catch everything?

Since Exception is the base class of all exceptions, it will catch any exception.

Which statement is true about catch () blocks?

catch block is executed only when exception is found. Here divide by zero exception is found hence both catch and finally are executed.

Which exception Cannot be caught in catch block?

Exceptions that Can't be Caught One such exception is the limit exception (System.


4 Answers

It is possible to catch Throwable. And yes, you will also catch instances of java.lang.Error which is a problem when it comes to e.g. OutOfMemoryError's. In general I'd not catch Throwable's. If you have to, you should do it at the highest place of your call stack, e.g. the main method (you may want to catch it, log it, and rethrow it).

I agree with your argumentation, it does not make sense to catch events you're not able to handle (e.g. OutOfMemoryError). A nice post is here.

like image 198
home Avatar answered Nov 07 '22 10:11

home


Yes it is possible to catch Throwable.

Whether the JVM / application will be able to continue if you catch an error depends on the actual error that occurred, what caused it, and what (if anything) your application does next.

  • In some cases, the JVM may be in such a bad way that no recovery is possible.

  • In some cases, the JVM is fine ... as long as you don't do certain things. Class loading errors are a good example. If your application doesn't attempt to use the class or dependent classes that you failed to load, you should be fine to continue. (In reality, applications are not designed to continue without classes that fail to load ... but if it was, it could.)

  • In some cases, the core JVM will be fine, but unspecified damage could have been done to the application's data structures, and/or to its threads and computation state. OOMEs are liable to do this to you, especially if your application is not designed to deal with threads that die unexpectedly.

  • Catching and recovering from OOMEs is problematic for another reason. While the termination of the current computation will free up some heap space, there's a good chance that it won't free up enough space. So you can easily get into a situation where your application is repeatedly throwing and catching lots of OOMEs, and making no real progress.

All of this means that it is generally a bad idea to try to recover from errors. And that's in line with what the javadoc for error says:

"An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch."

like image 45
Stephen C Avatar answered Nov 07 '22 08:11

Stephen C


  • Yes we can catch Throwable but as best practice, it is not advised to catch Throwable.
  • Catching Throwable includes Errors too, we should not catch errors, it helps to identify JVM issues.
like image 22
Premraj Avatar answered Nov 07 '22 09:11

Premraj


Throwable can be used instead of any individual exception/error. However it is recommended to pass through ThreadDeadException. So you can add extra catch clause for it, or filter by type.

like image 43
Dmitriy R Avatar answered Nov 07 '22 10:11

Dmitriy R