簡體   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