java从异常里获取入参信息_java – 使用Spring AOP处理异常时获取传递给方法的参数的值...
时间: 2024-01-28 14:03:44 浏览: 36
在 Java 中,可以通过捕获异常对象获取方法的入参信息。具体实现方式可参考以下代码示例:
```java
public void someMethod(String arg1, int arg2) {
try {
// some code here
} catch (Exception e) {
String arg1Value = arg1 != null ? arg1 : "null";
String arg2Value = String.valueOf(arg2);
String message = String.format("Exception occurred with arguments: arg1=%s, arg2=%s", arg1Value, arg2Value);
// log or handle the exception here with the message containing the argument values
}
}
```
如果使用 Spring AOP 处理异常,可以借助 `org.aspectj.lang.JoinPoint` 对象获取方法的入参信息。具体实现方式可参考以下代码示例:
```java
@Aspect
@Component
public class ExceptionHandlerAspect {
@AfterThrowing(pointcut = "execution(* com.example.somePackage.SomeClass.someMethod(..))", throwing = "ex")
public void handleException(JoinPoint joinPoint, Exception ex) {
Object[] args = joinPoint.getArgs();
String arg1Value = args[0] != null ? args[0].toString() : "null";
String arg2Value = args[1] != null ? args[1].toString() : "null";
String message = String.format("Exception occurred with arguments: arg1=%s, arg2=%s", arg1Value, arg2Value);
// log or handle the exception here with the message containing the argument values
}
}
```
以上代码示例仅供参考,具体实现方式可根据实际需求进行调整。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)