gpt4 book ai didi

java - 捕获运行时异常后程序停止执行

转载 作者:太空宇宙 更新时间:2023-11-04 09:36:58 24 4
gpt4 key购买 nike

我正在执行抛出 IllegalStateException 的代码。我想捕获该异常并继续执行程序,但是当遇到此异常时我的程序停止执行。

我尝试在多个位置放置 try{} catch{} block 。另外,尝试使用

Platform.runLater(new Runnable(){
@Override
public void run() { }
});

捕获此异常。

try {
List<ItemStandardFields> output = response.body().getContents();
Platform.runLater(new Runnable(){
@Override
public void run() { }
});
// some code
}catch(Exception ex){
log.error(ex.getLocalizedMessage());
}

实际 - 我的程序执行在输出 = ...getContents(); 处出现异常后停止;预期 - 我希望我的程序在捕获异常后继续执行。

最佳答案

您可以将代码放入新线程中并使用 Thread 类的 defaultUncaughtExceptionHandler。请检查以下代码。

 public static void main(String[] args) {

Thread thread = new Thread(new MyThread());

thread.setDefaultUncaughtExceptionHandler(new Thread.
UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
log.error(ex.getLocalizedMessage());
}
});
thread.start();


}
}

class MyThread implements Runnable {

public void run() {
List<ItemStandardFields> output = response.body().getContents();
}
}

关于java - 捕获运行时异常后程序停止执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56399095/

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