Java - Throwable to Exception


Java - Throwable to Exception



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.


exceptions


onError



for example one of my classes is throwing a NoSessionException. Can I check a throwable object if it is a NoSessionException ?


NoSessionException


NoSessionException





Throwable is the superclass of all exceptions. You can catch a throwable and then interrogate its class to see if it's a NoSessionException or whatever.
– Hot Licks
Sep 10 '12 at 20:47


catch





thank you all for the fast answer.
– Maik Klein
Sep 10 '12 at 20:48





Of course, you can also catch specifically NoSessionException, and then pass it to an interface that expects a Throwable -- since Throwable is the superclass the interface will accept NoSessionException.
– Hot Licks
Sep 10 '12 at 20:50





(And if you do catch all Throwables, you shouldn't simply ignore the ones you don't select, but rather you should re-throw them.)
– Hot Licks
Sep 10 '12 at 20:56




5 Answers
5



You can use instanceof to check it is of NoSessionException or not.


instanceof


NoSessionException



Example:


if (exp instanceof NoSessionException) {
...
}



Assuming exp is the Throwable reference.


exp


Throwable



Just make it short. We can pass Throwable to Exception constructor.


Throwable


Exception


@Override
public void onError(Throwable e) {
Exception ex = new Exception(e);
}



See this Exception from Android





This solved my problem.
– Naveed Ahmad
Jan 26 '17 at 7:22





@NaveedAhmad cool!
– THANN Phearum
Jan 26 '17 at 7:26





Stack trace data is lost doing this however :-(
– Brian Knoblauch
Feb 8 at 19:03





@BrianKnoblauch Android also provide another constructor to keep the StackTrace. Exception (String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace)
– THANN Phearum
Mar 27 at 14:39




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
}



In addition to checking if its an instanceof you can use the try catch and catch NoSessionException


instanceof


try {
// Something that throws a throwable
} catch (NoSessionException e) {
// Its a NoSessionException
} catch (Throwable t) {
// catch all other Throwables
}



Throwable is a class which Exception – and consequently all subclasses thereof – subclasses. There's nothing stopping you from using instanceof on a Throwable.


Throwable


Exception


instanceof


Throwable






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages