看了下try-catch-finally块的知识:
碰到了些问题:public class FinallyTest
{
public static void main(String[] args)
{
//System.out.println("main--- " + new FinallyTest().print());
} public int print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
return i;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
return i;
}
}
}
上面的代码不会报错。
可是如果我的print()方法没有返回值,也就是说print()的代码写成: public void print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
}
}就会报错!!!
Thread.sleep(1)块就要捕捉InterruptedException这个异常,
这是为什么啊?
碰到了些问题:public class FinallyTest
{
public static void main(String[] args)
{
//System.out.println("main--- " + new FinallyTest().print());
} public int print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
return i;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
return i;
}
}
}
上面的代码不会报错。
可是如果我的print()方法没有返回值,也就是说print()的代码写成: public void print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
}
}就会报错!!!
Thread.sleep(1)块就要捕捉InterruptedException这个异常,
这是为什么啊?
解决方案 »
- 怎么以顺序方式得到从jsp页面得到所有表单参数?
- JAVA如何修改ini配置文件中的指定字段的值?
- 如何输出10的4次方
- 乘法??
- 用firefox浏览一个网站的时候提示要安装JRE。jdk里面不是包含了JRE么?
- 在jpanel上绘图 如果图比较大 显示不完 想要做下拉条 高手帮忙指点下!谢谢
- 高手们!请问JAVA中如何获取主机MAC地址呢?
- final方法是最终方法不能改写,是什么意思?
- ※※※※※散风4※※※※※
- 各位大虾能帮我解决个问题吗?(一定给积分)
- 有一个单词库,随机抽出一个单词,把单词打乱,让用户拼写单词。
- 大家帮我看看这个简单代码,为什么system.out.println()输出多次。
还是就在这个类调用如果在这个类mian()直接调用print()方法
就应该把print()声明为Static
public class FinallyTest
{
public static void main(String[] args)
{
//System.out.println("main--- " + new FinallyTest().print());
} public int print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;//除以0,会有叫什么……byZero的异常
return i;
}
catch (IndexOutOfBoundsException e)//异常错误
{
}
finally
{
System.out.println("finally--- " + i);
return i;
}
}
}
我理解的是:只要是RuntimeException的话,放在catch()中,在编译时都是可以的。
这里IndexOutOfBoundsException和ArithmeticException的效果应该是一样的。
{
public static void main(String[] args)
{
//System.out.println("main--- " + new FinallyTest().print());
new FinallyTest().print();
} public void print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
//return;这个可写可不写
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
return;
}
}
}
但是如果把return写在try-catch-finally块外面,还是需要你捕捉InterruptedException的。
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
return;
}
}这样就不会报错了
所以catch 的就一定要是InterruptedExceptionIndexOutOfBoundsException和ArithmeticException是运行期异常,
而有返回值又不会报错呢。 public void print()
{
int i = -1;
try
{
Thread.sleep(1);
//i = 1 / 0;
//return i;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
//return i;
}
}输得代码会报错。
而如果最后一句的//return i;修改为return就不会报错了。
不知道为什么
{
public static void main(String[] args)
{
//System.out.println("main--- " + new FinallyTest().print());
} public void print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
return;//这里加个return
}
}
}
其实你的sleep()的异常InterruptedException是必须的捕捉的异常
但是 return如果是写在外边,try块里边出了异常,程序是没办法执行的了.
而在这个帖子里,对finally的理解又更加重要。
finally不管出现异常与否,都必须去执行的代码。
因此在代码中,必须执行finally中的代码
PS:如果在try或者catch中强行终止JVM,就不会运行finally中的代码了。然后再来看代码,
在你说的不会报错的情况下,
其实你把catch块注释掉也没问题的。
解释一下这个屏蔽:
在这种情况下,有finally块,并且在try块中出现了异常,
那么,finally块里的语句,是在执行了catch语句之后、退出方法之前运行的。
先把代码这样改一下:public void print() throws Exception {
int i = -1;
try {
Thread.sleep(1);
i = 1 / 0;
} catch (Exception e) {
System.out.println("catch step1.");
throw e;
} finally {
System.out.println("finally--- " + i);
return;
}
}以上的代码, 当程序运行到catch块中throw e时,其实并没有抛出异常,而是先把它放在栈中,
因为如果此时抛出了,那么方法就退出了。 在finally块中有return语句,所以方法正常退出。也没有抛出异常。
PS:
1.你可以打印一下,在进入catch快后,打印了catch step1. 但并没有finally--- -1输出。
2.你可以运行一下,看是否这个被我改过的程序有异常抛出,肯定没有,而确实有异常产生了。
它还要去执行finally块中的代码。
因为如果此时抛出了,那么方法就退出了。但这时候finally块还未必执行。
而在finally块中恰好有return语句,所以方法正常退出。也就没有抛出异常。
不过为什么return会屏蔽掉异常呢
你的表述好混乱啊