Java异常处理程序整个类

huangapple go评论79阅读模式
英文:

Java ExceptionHandler whole Class

问题

以下是翻译好的部分:

有没有一种方法可以为一个类和异常类设置ExceptionHandler?
我有一个像这样的类:

public class MyServiceClass {

	public void foo() {
    	//一些代码... 
	    throw new MyCustomRuntimeException();
    	//更多的代码... 
	}

    public void foo2() {
	    //一些其他的代码... 
	    throw new MyCustomRuntimeException();
	    //更多的其他代码... 
	}
}

现在我想定义一个类似于这样的MyCustomRuntimeException处理程序:

private void exceptionHandler(MyCustomRuntimeException ex) {
    //一些魔法
}

每当在这个类中抛出MyCustomRuntimeException时都应该使用它。我知道我可以在每个方法中使用try、catch、finally,但是否有一种类范围的解决方案?想要跳过样板代码

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

我在这个应用程序中使用了Spring(不是Spring Boot),但我找不到如何在纯Spring中使用@ExceptionHandler的方法。我尝试了以下方法(不起作用):

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();
	}
}
英文:

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();
	}
}

答案1

得分: 1

如果您没有使用spring-mvc且不在多线程环境中,您可以使用以下代码。

public class ExceptionHandler implements Thread.UncaughtExceptionHandler {

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

然后在您的main方法中添加这行代码。这对于小型应用程序效果良好,Spring在这里的作用很小。

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

如果您有一个较大的应用程序并且需要更优雅的解决方案 - 请考虑在您的应用程序中引入切面(AOP)。

编辑于2020年6月2日


当使用spring-mvc时

您可以使用@ExceptionHandler来处理这个问题。Spring教程

@ExceptionHandler可以处理类特定的和全局的处理程序(通过@ControllerAdvice

类特定的处理程序在全局处理程序之前触发。因此最佳实践是在全局处理程序中使用RuntimeExceptionException,而不是在各个类中使用它们。进一步减少样板代码。

英文:

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.

huangapple
  • 本文由 发表于 2020年5月29日 22:49:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/62088688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定