exception.getMessage() output with class name

前端 未结 2 460
天涯浪人
天涯浪人 2020-12-16 10:03

I\'m trying to fix an issue, in my application I have this code

try {
  object1.method1();
} catch(Exception ex) {
   JOptionPane.showMessageDialog(nulll, \"         


        
2条回答
  •  情话喂你
    2020-12-16 10:23

    My guess is that you've got something in method1 which wraps one exception in another, and uses the toString() of the nested exception as the message of the wrapper. I suggest you take a copy of your project, and remove as much as you can while keeping the problem, until you've got a short but complete program which demonstrates it - at which point either it'll be clear what's going on, or we'll be in a better position to help fix it.

    Here's a short but complete program which demonstrates RuntimeException.getMessage() behaving correctly:

    public class Test {
        public static void main(String[] args) {
            try {
                failingMethod();
            } catch (Exception e) {
                System.out.println("Error: " + e.getMessage());
            }
        }       
    
        private static void failingMethod() {
            throw new RuntimeException("Just the message");
        }
    }
    

    Output:

    Error: Just the message
    

提交回复
热议问题