Printing Exception Message in java

后端 未结 2 1599
太阳男子
太阳男子 2020-12-18 18:50

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          


        
相关标签:
2条回答
  • 2020-12-18 19:10
    try {
    } catch (javax.script.ScriptException ex) {
    // System.out.println(ex.getMessage());
    }
    
    0 讨论(0)
  • 2020-12-18 19:17

    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.

    0 讨论(0)
提交回复
热议问题