I am currently using the play2 framework.
I have several classes which are throwing exceptions
but play2s global onError
handler uses throwable instead of an exception.
for example one of my classes is throwing a NoSessionException
. Can I check a throwable object if it is a NoSessionException
?
We can pass Throwable to Exception constructor.
Using the Throws keyword Throws is a keyword used to indicate that this method could throw this type of exception. The caller has to handle the exception using a try-catch block or propagate the exception. We can throw either checked or unchecked exceptions.
Don't Catch Throwable Throwable is the superclass of all exceptions and errors. You can use it in a catch clause, but you should never do it! If you use Throwable in a catch clause, it will not only catch all exceptions; it will also catch all errors.
The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement.
Just make it short. We can pass Throwable
to Exception
constructor.
@Override
public void onError(Throwable e) {
Exception ex = new Exception(e);
}
See this Exception from Android
You can use instanceof
to check it is of NoSessionException
or not.
Example:
if (exp instanceof NoSessionException) {
...
}
Assuming exp
is the Throwable
reference.
Can I check a throwable object if it is a NoSessionException ?
Sure:
Throwable t = ...;
if (t instanceof NoSessionException) {
...
// If you need to use information in the exception
// you can cast it in here
}
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