首页 > 解决方案 > 如何添加 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());
    }
}

标签: javaexception

解决方案


如果你在 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");

    }
}

推荐阅读