如果下面的方法出现问题,是否可以在返回结果中添加异常?
我尝试了以下方法,但收到验证错误消息
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 block 中返回它会怎么样,例如
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");
}
}
关于java - 如何添加 e.getmessage() 作为 java 方法的返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50876588/