在java中打印异常消息 [英] Printing Exception Message in java

查看:187
本文介绍了在java中打印异常消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我尝试以下代码片段时:

  try {
//语句
} catch(javax.script.ScriptException ex){
System.out.println ex.getMessage());
}

输出是:

 无效的JavaScript代码:sun.org.mozilla.javascript.internal.EvaluatorException:
missing}属性列表(< Unknown source>)在< Unknown source> ;
在行号1

有没有办法打印消息没有异常信息,来源和行号信息。换句话说,我想在输出中打印的消息是:

  missing} after property list 


解决方案

输出对我来说是正确的:


$未知来源中的属性列表(<未知来源>)之后无效的JavaScript代码:sun.org.mozilla.javascript.internal.EvaluatorException:missing} after $< Unknown source> ;;在行号1

我认为无效的Javascript代码:.. 是异常消息的开始。



通常情况下不会返回堆栈跟踪信息:

 尝试{
抛出新的RuntimeException(hu?\\\
trace-line1\\\
trace-line2);
} catch(Exception e){
System.out.println(e.getMessage()); //打印hu?
}

所以也许您正在调用的代码捕获异常并重新启动一个 ScriptException 。在这种情况下,可能 e.getCause()。getMessage()可以帮助您。


Is there a way to print an exception message in Java without the exception?

When I try the following piece of code:

try {
    // statements
} catch (javax.script.ScriptException ex) {
    System.out.println(ex.getMessage());
}

The output is:

Invalid JavaScript code: sun.org.mozilla.javascript.internal.EvaluatorException:
missing } after property list (<Unknown source>) in <Unknown source>; 
at line number 1

Is there a way to print the message without the exception information, source and line number information. In other words, the message I would like to print in the output is:

missing } after property list

解决方案

The output looks correct to me:

Invalid JavaScript code: sun.org.mozilla.javascript.internal.EvaluatorException: missing } after property list (<Unknown source>) in <Unknown source>; at line number 1

I think Invalid Javascript code: .. is the start of the exception message.

Normally the stacktrace isn't returned with the message:

try {
    throw new RuntimeException("hu?\ntrace-line1\ntrace-line2");
} catch (Exception e) {
    System.out.println(e.getMessage()); // prints "hu?"
}

So maybe the code you are calling catches an exception and rethrows a ScriptException. In this case maybe e.getCause().getMessage() can help you.

这篇关于在java中打印异常消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆