java - 如何添加 e.getmessage() 作为 java 方法的返回

标签 java exception

如果下面的方法出现问题,是否可以在返回结果中添加异常?

我尝试了以下方法,但收到验证错误消息

 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/

相关文章:

java - 如何映射具有接口(interface)和类的方法——从操作到委托(delegate)

java - 有没有办法按需打印堆栈跟踪?

java - 无法使用 VisualVM 连接到远程 JMX,但可以使用 JConsole

java - Swing 中的 JFrame 窗口是否运行在它们自己的独立线程上?

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

java - java中的输入不匹配错误

java - Linux\BSD 网络编程

c# - 什么是后置条件异常?

java.lang.NullPointerException 和 java.net.SocketException

objective-c - 核心数据和 NSEntityDescription