Java - Throwable to Exception
Asked Answered
B

6

50

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 ?

Bunnybunow answered 10/9, 2012 at 20:45 Comment(4)
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.Lactoprotein
thank you all for the fast answer.Bunnybunow
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.Lactoprotein
(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.)Lactoprotein
F
53

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

Example:

if (exp instanceof NoSessionException) {
...
}

Assuming exp is the Throwable reference.

Farmann answered 10/9, 2012 at 20:46 Comment(0)
K
56

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

Khoury answered 12/1, 2017 at 7:43 Comment(2)
Stack trace data is lost doing this however :-(Honewort
@BrianKnoblauch Android also provide another constructor to keep the StackTrace. Exception (String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace)Khoury
F
53

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

Example:

if (exp instanceof NoSessionException) {
...
}

Assuming exp is the Throwable reference.

Farmann answered 10/9, 2012 at 20:46 Comment(0)
A
16

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
}
Aureaaureate answered 10/9, 2012 at 20:46 Comment(0)
E
6

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

Erased answered 10/9, 2012 at 20:46 Comment(0)
S
6

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

try {
    // Something that throws a throwable
} catch (NoSessionException e) {
    // Its a NoSessionException 
} catch (Throwable t) {
    // catch all other Throwables
}
Syndic answered 10/9, 2012 at 20:51 Comment(0)
D
0

If you are using Spring, then why not go for a specific exception handler for specific type.

You can have one GlobalException handler and tag it to your controller

@ControllerAdvice(assignableTypes = {YourController.class})
public class MyGlobalExceptionHandler {

Then you can have specific handler methods to handle specific exception.

for e.g. below is handling specific ServerWebInputException

  @ExceptionHandler(ServerWebInputException.class)
      public ResponseEntity<YourResponseObject> handleValidationError(final ServerWebInputException badRequest) {
    }

and you can have a generic Throwable handler

@ExceptionHandler(Throwable.class)
  public ResponseEntity<YourResponseObject> handleThrowable(final Throwable throwable) {

This would be a clean solution.

Deas answered 14/6, 2022 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.