我试图FileNotFoundException
通过将线程挂起 x 秒并重新读取文件来处理 Java 中的一个。这背后的想法是在运行时编辑属性。
问题是程序只是终止了。知道如何实现这个解决方案吗?
我试图FileNotFoundException
通过将线程挂起 x 秒并重新读取文件来处理 Java 中的一个。这背后的想法是在运行时编辑属性。
问题是程序只是终止了。知道如何实现这个解决方案吗?
有一个古老的配方,最初由 Bjarne Stroustroup 为 C++ 编写,在这里移植到 Java:
Result tryOpenFile(File f) {
while (true) {
try {
// try to open the file
return result; // or break
} catch (FileNotFoundException e) {
// try to recover, wait, whatever
}
}
}
循环加载文件,并在文件成功读取后设置条件所依赖的变量。在循环内使用 try-catch 块并在 catch-block 中等待。
一些代码片段会很有用,但以下问题之一可能是问题所在:
catch (Exception e)
看看抛出了什么异常祝你好运
如果从未捕获到异常,则终止线程。如果这是您的主线程,则应用程序结束。尝试以下操作:
try
{
props.load(...);
}
catch (FileNotFoundException ex)
{
Thread.sleep(x * 1000);
props.load(...);
}