简体   繁体   English

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

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

Is it possible to add the exception in the return result incase of any issues in the below method? 如果以下方法有任何问题,是否可以在返回结果中添加异常?

I tried the below way but receiving validation error message 我尝试了以下方式,但收到验证错误消息

 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());
    }
}

What if you return it in the catch block, like 如果您将其返回到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");

    }
}

Chagne code as below 香槟代码如下

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

    }

or you can return error message directly from catch. 或者您可以直接从catch返回错误消息。

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

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

相关问题 e.getMessage().getContentDisplay() 返回空字符串 - e.getMessage().getContentDisplay() returns empty String 异常方法e.getMessage()和e.getLocalizedMessage()显示程序包 - Exception methods e.getMessage() and e.getLocalizedMessage() are showing package e.getMessage() 和 e.getLocalizedMessage() 之间的区别 - Difference between e.getMessage() and e.getLocalizedMessage() LOG.error(“ IOException:” + e.getMessage(),e)之间有什么区别? 和LOG.error(“ IOException:” + e.getMessage());? - What is the difference between LOG.error(“IOException: ” + e.getMessage(), e); and LOG.error(“IOException: ” +e.getMessage());? 使用jtds连接到SQL Server 2008时e.getMessage()返回null - e.getMessage() returns null when using jtds to connect to an SQL Server 2008 如何用E ...元素编写Java泛型方法并返回List <E> - How to write Java generic method with E… elements and return List<E> JAVA异常getMessage()方法调用构造函数 - JAVA exception getMessage() method calling constructor 如何更改Exception.getMessage()的返回消息? - How to change the return message of Exception.getMessage()? 如何正确使用异常方法getMessage - How to properly use Exception method getMessage 如何将Java ChangeListener添加到方法返回 - How to add a Java ChangeListener to a method return
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM