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.
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.
Since Exception is the base class of all exceptions, it will catch any exception.
catch block is executed only when exception is found. Here divide by zero exception is found hence both catch and finally are executed.
Exceptions that Can't be Caught One such exception is the limit exception (System.
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.
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."
Throwable
but as best practice, it is not advised to catch Throwable
. Throwable
includes Errors
too, we should not catch errors, it helps to identify JVM issues.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With