public void function()
{
int b = 0;
try {
if (b == 0) {
throw new Exception("b == 0");
}
} catch (Exception e) {
System.out.println(e.getMessagee());
} System.out.println("It is over");
}请问 try-catch 后的打印语句还执行吗,即是不是还要输出It is over?
为什么?
{
int b = 0;
try {
if (b == 0) {
throw new Exception("b == 0");
}
} catch (Exception e) {
System.out.println(e.getMessagee());
} System.out.println("It is over");
}请问 try-catch 后的打印语句还执行吗,即是不是还要输出It is over?
为什么?
System.out.println(e.getMessagee());
然后结束
打印结果是
b==0
再执行 System.out.println("It is over");
It is over
{ public static void main(String[] args)
{
int b = 0;
try {
if (b == 0) {
throw new Exception("b == 0");
}
System.out.println("cann't execute!"); //这句不会执行
} catch (Exception e) {
System.out.println(e.getMessage());
} System.out.println("It is over");
}
}发生异常会只是 try 所包含的发生异常后面的语句不会被执行。个人理解!
你捕获异常了,就不会再传递出去了,执行异常处理逻辑后,程序回到异常发生点,继续往下执行。
如果不捕获异常的话,It is over就不会打印出来了
{
int b = 0;
try {
if (b == 0) {
throw new Exception("b == 0");这句引发异常类,跳到catch块继续执行 }
} catch (Exception e) {
System.out.println(e.getMessagee());第二步就到这里了
} System.out.println("It is over"); 第三步就到这里,catch 处理完会继续执行 }
我试了:
b==0
It is over
是正确的结果
System.out.println(e.getMessagee()); (2)
}把try里面的异常自己处理了。我觉得简单可以理解为 if(异常){catch 执行}
对~~直接从try跳转到catch然后顺序执行catch下面的语句