java - 如何添加 e.getmessage() 以换取 java 方法
问题描述
如果以下方法中出现任何问题,是否可以在返回结果中添加异常?
我尝试了以下方式,但收到验证错误消息
Multiple markers at this line
- e cannot be resolved to a variable
- e cannot be resolved
@RestController
public class RunnerController {
@RequestMapping(value="/run", method=RequestMethod.POST)
public String runScript(@RequestParam Integer order) {
try{
//some code here//
} catch (Exception e) {
log.error("An unexpected error occurred when attempting to run script. The error was: " + e.getMessage() + ".", e);
}
return ("An unexpected error occurred running script. The error was: " + e.getMessage());
}
}
解决方案
如果你在 catch 块中返回它怎么办,比如
public class RunnerController {
@RequestMapping(value="/run", method=RequestMethod.POST)
public String runScript(@RequestParam Integer order) {
try{
//some code here//
} catch (Exception e) {
log.error("An unexpected error occurred when attempting to run script. The error was: " + e.getMessage() + ".", e);
return ("An unexpected error occurred running script. The error was: " + e.getMessage());
}
return ("No unexpected error occurred running script");
}
}
推荐阅读
- javascript - 在打字稿中的多维数组中查找索引
- javascript - 使用 reactjs 将数据保存在表单中
- java - 在android中拨打电话号码时缺少括号
- c# - ASP.NET Core 3.1 - EF Core 无法翻译 GroupBy 和 Join
- python - Kivy - 从弹出窗口自动启动方法
- linux - 如何从已发布的项目中为 Linux 构建安装程序文件
- ios - Swift - snapshotView 功能不适用于大型 UIView
- java - 如何为每种方法生成带有子测试的范围报告?
- python - 如何在python中创建两个组元素之间的映射?
- python - 在 python 中使用 IN 子句插入一个值时出错