java中异常捕获后,不用java catch 抛出异常直接使用finally这样的语句合法吗?

9363人阅读
java(15)
情况1:try块中没有抛出异常try和finally块中都有return语句
public static int NoException(){
System.out.println(&i in try block is&+i);
return --i;
}catch(Exception e){
System.out.println(&i in catch - form try block is&+i);
return --i;
System.out.println(&i in finally - from try or catch block is&+i);
return --i;
执行结果:
i in try block is10
i in finally - from try or catch block is9
the method value is8
执行顺序:执行try块,执行到return语句时,先执行return的语句,--i,但是不返回到main 方法,执行finally块,遇到finally块中的return语句,执行--i,并将值返回到main方法,这里就不会再回去返回try块中计算得到的值
情况2:try块中没有抛出异常,仅try中有return语句
public static int NoException(){
System.out.println(&i in try block is--&+i);
return --i;
}catch(Exception e){
System.out.println(&i in catch - form try block is--&+i);
return --i;
System.out.println(&i in finally - from try or catch block is--&+i);
System.out.println(&i in finally block is--&+i);
//return --i;
执行结果:
i in try block is--10
i in finally - from try or catch block is--9
i in finally block is--8
the method value is--9
顺序:try中执行完return的语句后,不返回,执行finally块,finally块执行结束后,返回到try块中,返回i在try块中最后的值
情况3:try块中抛出异常try,catch,finally中都有return语句
public static int WithException(){
System.out.println(&i in try block is--&+i);
return --i;
}catch(Exception e){
System.out.println(&i in catch - form try block is--&+i);
System.out.println(&i in catch block is--&+i);
return --i;
System.out.println(&i in finally - from try or catch block is--&+i);
System.out.println(&i in finally block is--&+i);
return --i;
执行结果:
i in try block is--10
i in catch - form try block is--10
i in catch block is--9
i in finally - from try or catch block is--8
i in finally block is--7
the method value is--6
顺序,抛出异常后,执行catch块,在catch块的return的--i执行完后,并不直接返回而是执行finally,因finally中有return语句,所以,执行,返回结果6
情况4,catch中有return,finally中没有,同上,执行完finally语句后,依旧返回catch中的执行return语句后的值,而不是finally中修改的值
情况5:try和catch中都有异常,finally中无return语句
public static int CatchException(){
System.out.println(&i in try block is--&+i);
return --i;
}catch(Exception e){
System.out.println(&i in catch - form try block is--&+i);
int j = i/0;
return --i;
System.out.println(&i in finally - from try or catch block is--&+i);
System.out.println(&i in finally block is--&+i);
//return --i;
i in try block is--10
i in catch - form try block is--10
i in finally - from try or catch block is--10
i in finally block is--9
Exception in thread &main& java.lang.ArithmeticException: / by zero
&at exception.ExceptionTest0123.CatchException(ExceptionTest0123.java:29)
&at exception.ExceptionTest0123.main(ExceptionTest0123.java:17)
执行顺序:在try块中出现异常,到catch中,执行到异常,到finally中执行,finally执行结束后判断发现异常,抛出
情况6:try,catch中都出现异常,在finally中有返回
public static int CatchException(){
System.out.println(&i in try block is--&+i);
return --i;
}catch(Exception e){
System.out.println(&i in catch - form try block is--&+i);
int j = i/0;
return --i;
System.out.println(&i in finally - from try or catch block is--&+i);
System.out.println(&i in finally block is--&+i);
return --i;
运行结果:
i in try block is--10
i in catch - form try block is--10
i in finally - from try or catch block is--10
i in finally block is--9
the method value is--8
执行顺序:try块中出现异常到catch,catch中出现异常到finally,finally中执行到return语句返回,不检查异常
没有catch,只有try和finally时,执行顺序和上面的几种情况差不多,只是少了catch块的执行
欢迎大家补充指正
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:123651次
积分:1866
积分:1866
排名:千里之外
原创:62篇
评论:49条
(1)(1)(1)(1)(1)(9)(5)(1)(5)(4)(8)(17)(3)(1)(9)今天,学习了try-catch-finally语句,本来觉得蛮简单、易懂的。搜了一道相关类型的题。结果信心被泼了盆冷水。先把题Mark一下,出去透透风。 1publicclass TestEx {
2privateint
3 4public TestEx() {
@SuppressWarnings("finally")
8boolean testEx() throws Exception {
9boolean ret = true;
ret = testEx1();
} catch (Exception e) {
System.out.println("testEx, catch exception");
ret = false;
} finally {
System.out.println("testEx, return value=" + ret);
@SuppressWarnings("finally")
23boolean testEx1() throws Exception {
24boolean ret = true;
ret = testEx2();
27if (!ret) {
28returnfalse;
System.out.println("testEx1, at the end of try");
} catch (Exception e) {
System.out.println("testEx1, catch exception");
ret = false;
} finally {
System.out.println("testEx1, return value=" + ret);
@SuppressWarnings("finally")
43boolean testEx2() throws Exception {
44boolean ret = true;
46int b = 12;
47for (int i = 2; i &= -2; i--) {
setC(b / i);
System.out.println("i=" + i);
51returntrue;
} catch (Exception e) {
System.out.println("testEx2, catch exception");
ret = false;
} finally {
System.out.println("testEx2, return value=" + ret);
6162publicstaticvoid main(String[] args) {
TestEx testException1 = new TestEx();
testException1.testEx();
} catch (Exception e) {
e.printStackTrace();
7071publicint getC() {
7475publicvoid setC(int c) {
76this.c =
78 }Output:看完之后我就在想,1.Java异常处理机制,我真的理解了吗?2.Java异常处理,我真的掌握了吗?3.catch体里遇到return 是怎么处理?4.finally 体里有return怎么处理?5. catch 和 finally 体里同时遇上 return 怎么办?6.是不是还有个System.exit()?遇到它又咋办??7.仅仅知道throws不是完全之策啊,还要继续深入理解啊。8:21:08
以上就介绍了 Java异常捕获之一道try-catch-finally语句题,包括了方面的内容,希望对Java教程有兴趣的朋友有所帮助。
本文网址链接:/article/detail_4365369.html
上一篇: 下一篇:java面试必备之异常处理机制 -
- ITeye博客
博客分类:
JAVA异常机制
一、基本概念
在Java中这种在程序中运行时可能出现的一些错误称为异常。Java处理异常时,如果某个方法抛出异常,既可以在当前方法中进行捕捉,然后处理该异常,也可以将异常向上抛出,由方法调用者来处理。异常产生后,如果不做任何处理,程序就会被终止。
二、异常捕获和处理
1、Java异常处理涉及到五个关键字,分别是:try、catch、finally、throw、throws。
Try:可能发生异常的Java语句
Catch:激发被捕获的异常
finally:方法返回前总要执行的代码.
throw:用于抛出一个异常对象
throws:在声明方法时,用于指定该方法可能抛出的异常。
2、try、catch、finally三个语句块应注意的问题(笔试重点)
第一、try、catch、finally三个语句块均不能单独使用,三者可以组成 try...catch...finally、try...catch、try...finally三种结构,catch语句可以有一个或多个,finally语句最多一个。
第二、try、catch、finally三个代码块中变量的作用域为代码块内部,分别独立而不能相互访问。如果要在三个块中都可以访问,则需要将变量定义到这些块的外面。
第三、多个catch块时候,只会匹配其中一个异常类并执行catch块代码,而不会再执行别的catch块,并且匹配catch语句的顺序是由上到下。
第四、无论程序是否有异常,并且无论之间try-catch是否顺利执行完毕,都会执行finally语句。在以下特殊情况下,finally块不会执行:在finally语句块中发生异常;在前面代码中使用了System.exit()退出程序;程序所在线程死亡;关闭cpu。
第五、当程序执行try块,catch块时遇到return语句或者throw语句,这两个语句都会导致该方法立即结束,所以系统并不会立即执行这两个语句,而是去寻找该异常处理流程中的finally块,如果没有finally块,程序立即执行return语句或者throw语句,方法终止。如果有finally块,系统立即开始执行finally块,只有当finally块执行完成后,系统才会再次跳回来执行try块、catch块里的return或throw语句,如果finally块里也使用了return或throw等导致方法终止的语句,则finally块已经终止了方法,不用再跳回去执行try块、catch块里的任何代码了。
3、throw与throws
1、throws出现在方法函数头;而throw出现在函数体。throws表示出现异常的一种可能性,并不一定会发生这些异常;throw则是抛出了异常,执行throw则一定抛出了某种异常对象。
三、异常结构
Throwable类派生了两个子类。Error类用来描述Java运行系统中的内部错误以及资源耗尽的错误;Exception类为非致命性类,可以通过捕捉处理使程序继续执行。Exception类根据错误发生的原因分为运行时异常和检查异常。如图所示。
1、检查异常
检查异常是RuntimeException以外的异常(IOException、SQLException等),必须显示的地处理否则无法编译通过。处理方法有两种,一是用try...catch捕捉异常,二是使用throws声明抛出该异常。
2、运行时异常
运行时异常的特点是Java编译器不会检查它,程序中可以选择捕获处理,也可以不处理。
Error(错误):是程序无法处理的错误,表示代码运行时 JVM(Java 虚拟机)出现的问题。例如,Java虚拟机运行错误(Virtual MachineError),当 JVM 不再有继续执行操作所需的内存资源时,将出现 OutOfMemoryError。
四、自定义异常
自定义异常只需编写一个类继承Exception类(Throwable及其子类均可)即可。
&!--EndFragment--&
不笑不是不开心
浏览: 4033 次
天天 造轮子}

我要回帖

更多关于 java catch finally 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信