Java - thrown to exception

I am currently using the play2 framework.

I have several classes that throw exceptions , but the play2s global onError uses throwable instead of an exception.

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

+8
source share
5 answers

You can use instanceof to verify that it has a NoSessionException value or not.

Example:

 if (exp instanceof NoSessionException) { ... } 

Assuming exp is a Throwable reference.

+22
source

Just do it shortly. We can pass the Throwable constructor to an Exception .

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

See Exception with Android

+12
source

Can I check the throw object if this is a NoSessionException?

Of course:

 Throwable t = ...; if (t instanceof NoSessionException) { ... // If you need to use information in the exception // you can cast it in here } 
+8
source

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

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

Throwable is a class that is Exception - and therefore all its subclasses are subclasses. There is nothing stopping you from using instanceof on Throwable .

+2
source

Source: https://habr.com/ru/post/1208290/


All Articles