Jonny
Jonny

Reputation: 100

Java ExceptionHandler whole Class

Is there a way to set a ExceptionHandler for a Class and Exceptionclass? I have a class like this:

public class MyServiceClass {

    public void foo() {
        //some Code... 
        throw new MyCustomRuntimeException();
        //some more Code... 
    }

    public void foo2() {
        //some other Code... 
        throw new MyCustomRuntimeException();
        //more other Code... 
    }
}

Now I would like to define a MyCustomRuntimeException - Handler something like this:

private void exceptionHandler(MyCustomRuntimeException ex) {
    //some Magic
}

Which should be used everytime a MyCustomRuntimeException is thrown in this class. I know I could use try, catch, finally in each method, but is there a Class wide solution? Would like to skip the boilerplate

try {
...
} catch (MyCustomRuntimeException ex) {
    exceptionHandler(ex);
}

Iam using Spring in this application (no Spring Boot), but I found nothing how to use @ExceptionHandler for plain Spring. I tried the following (doesn`t work):

EasyApplication

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class EasyApplication {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
        FooBar foo = context.getBean(FooBar.class);
        foo.doException();
    }
}

FooBar

import org.springframework.web.bind.annotation.ExceptionHandler;

public class FooBar {

    public void doException() {
        throw new RuntimeException();
    }

     @ExceptionHandler(value = RuntimeException.class)
     public void conflict() {
         System.out.println("Exception handled!");
     }
}

MyConfiguration

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfiguration {

    @Bean(name = "FooBar")
    public FooBar fooBar() {
        return new FooBar();
    }
}

Upvotes: 1

Views: 76

Answers (1)

aksappy
aksappy

Reputation: 3400

If you are not using spring-mvc and not in a multi-threaded environment, you would be able to do well with the following.

public class ExceptionHandler implements Thread.UncaughtExceptionHandler {

    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("This is from the uncaught");
    }

}

And then add this line in your main method. This works for small applications, and spring has minimal role here.

Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler());

If you have a bigger application and needs a more elegant solution - look to introduce aspects (AOP) in to your app.

Edited June 2nd 2020


This is when spring-mvc is used

You can use @ExceptionHandler for this. Spring Tutorial

@ExceptionHandler can handle both class specific and global handlers (via @ControllerAdvice)

The class specific handlers is triggered before global handlers. So best practice is to use RuntimeException and Exception in global handlers, instead of using them in individual classes. Reduces boilerplate further.

Upvotes: 1

Related Questions