48

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 ?

Maik Klein
  • 15,548
  • 27
  • 101
  • 197
  • 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. – Hot Licks Sep 10 '12 at 20:47
  • 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
  • 1
    (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

6 Answers6

53

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

Example:

if (exp instanceof NoSessionException) {
...
}

Assuming exp is the Throwable reference.

kosa
  • 65,990
  • 13
  • 130
  • 167
52

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

toto_tata
  • 14,526
  • 27
  • 108
  • 198
THANN Phearum
  • 1,969
  • 22
  • 19
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
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
6

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

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
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
}
km1
  • 2,383
  • 1
  • 22
  • 27
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.

Sanjay Bharwani
  • 3,317
  • 34
  • 31