public class ExceptionTest {
public static void main(String[] args) {
int i = 0;
try {
if(i == 0) {
throw new IOException();
}
} catch (IOException e) {
System.out.println("right!");
}
}
}
运行正常, 输出正确结果: right!。
本文通过一个简单的Java程序示例介绍了如何使用try-catch块来处理IOException。当条件满足时,程序将抛出异常并捕获它,最终输出预设的消息。
public class ExceptionTest {
public static void main(String[] args) {
int i = 0;
try {
if(i == 0) {
throw new IOException();
}
} catch (IOException e) {
System.out.println("right!");
}
}
}
运行正常, 输出正确结果: right!。
361

被折叠的 条评论
为什么被折叠?