gpt4 book ai didi

java - 获取异常对象的错误号

转载 作者:行者123 更新时间:2023-12-02 20:11:07 24 4
gpt4 key购买 nike

我开发了一个程序,它有一个入口点。它周围有一个 Try catch block 。

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();
}

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

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

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

最佳答案

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

public static int getLineNumber() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();
}

虽然建议使用日志库,例如 log4j .

关于java - 获取异常对象的错误号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11094396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com