繁体   English   中英

如何添加e.getmessage()作为Java方法的回报

[英]how to add the e.getmessage() in return for a java method

如果以下方法有任何问题,是否可以在返回结果中添加异常?

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

 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");

    }
}

香槟代码如下

 @RequestMapping(value="/run", method=RequestMethod.POST)    
    public String runScript(@RequestParam Integer order) {     
    String message = null;
        try{
            //some code here//
    } catch (Exception e) { 
      message = e.getMessage();
        log.error("An unexpected error occurred when attempting to run script. The error was: " + e.getMessage() + ".", e);
    }
        return message != null ? message : "something that you want to return";

    }

或者您可以直接从catch返回错误消息。

您应该在catch返回异常e ,因为在外部不可见

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM