[英]e.getMessage().getContentDisplay() returns empty String
[英]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.