sách gpt4 ai đã đi

java - 抛出一个有意的异常以停止在 Java 中执行程序

In lại 作者:行者123 更新时间:2023-11-30 10:53:36 41 4
mua khóa gpt4 Nike

我故意放置一个异常来停止程序执行,但似乎在抛出异常后继续执行。

try{
System.out.println("Executing ");
throw new RuntimeException("This is thrown intentionally");
} catch {....
}
System.out.println("Must not execute");

我不想使用 System.exit() 因为我不想停止 JVM 也不想使用 trở lại 因为我想要它看起来像发生了错误。请帮助解决这个问题。

1 Câu trả lời

抛出异常后不需要捕获异常。如果您在那里捕获它,程序将开始执行 catch block 之后的语句。

System.out.println("Executing ");
if(1 == 1) // I put this 'if' here so that the code compiles
{
throw new RuntimeException("This is thrown intentionally");
}
System.out.println("Must not execute");

关于java - 抛出一个有意的异常以停止在 Java 中执行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33935044/

41 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com