获取异常对象的错误号

Cha*_*han 2 java exception nullpointerexception

我开发了一个程序,它有一个入口点.Try catch块围绕着它.

try {
            Runner runner = new Runner();
            // Adhoc code
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
            runner.setupVariables();
            runner.setLookAndFeel();
            runner.startSessionFactory();
            runner.setupApplicationVariables();
            runner.setupDirectories();
            // This will be used to test out frames in development mode
            if (Runner.isProduction == true) {
                execute();
            } else {
                test();
            }
        } catch (Exception e) {
                SwingHelper.showErrorMessageMainFrame(e.getMessage());
            Logger.getRootLogger().error(e);
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

但是假设抛出空指针异常,消息框为空,因为Exception不包含消息.为此,我添加了一个逻辑 -

 if(e instanceof NullPointerException){
        NullPointerException n =(NullPointerException) e;
        SwingHelper.showErrorMessageMainFrame("Unexpected Exception due at ");
    }else{
SwingHelper.showErrorMessageMainFrame(e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)

这一切都很好,但我也希望显示行号.我怎样才能完成它.如何获取异常的行号?

jam*_*ond 5

在这个问题的答案中,您可以使用此代码段:

public static int getLineNumber() {
    return Thread.currentThread().getStackTrace()[2].getLineNumber();
}
Run Code Online (Sandbox Code Playgroud)

建议使用log4j等日志库.