我敢肯定这一定是早些时候问过的,但我无法搜索帖子。
我想使用 native Java 库捕获由线程生成的运行时错误,我可以使用哪些方法来做到这一点?
这是一个错误的例子:
Exception in thread "main" java.io.FileNotFoundException: C:\Documents and Settings\All Users\Application Data\CR2\Bwac\Database\BWC_EJournal.mdb (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at CopyEJ.CopyEJ.main(CopyEJ.java:91)
我想把这个错误记录在文件中,以便稍后查看
最佳答案
只捕获异常!我的猜测是,目前您的 main
方法被声明为抛出 Exception
,而您没有捕捉到任何东西...所以异常只是从 main 传播出去
。只捕获异常:
try {
// Do some operations which might throw an exception
} catch (FileNotFoundException e) {
// Handle the exception however you want, e.g. logging.
// You may want to rethrow the exception afterwards...
}
参见 exceptions part of the Java tutorial有关异常的更多信息。
native 代码中出现异常的事实在这里无关紧要 - 它以完全正常的方式传播。
关于java - 捕获运行时错误 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17441261/