a.try和catch总有一个要执行;
b.finally总在try或者catch之后执行;
c.某一层捕获了异常,这一层的try代码区中出现异常之后的代码将不被执行
import java.io.File;
import java.io.FileNotFoundException;
public class TestException {
public static void main(String[] args) {
new AA().test1();
}
}
class AA {
public int test1(){
try {
int j= new AA().test2();
System.out.println("try of ...test1");
return j;
} catch (Exception e) {
System.out.println("catch of ...test1");
e.printStackTrace();
return 0;
} finally {
System.out.println("finally of ...test1");
}
}
public int test2() {
try {
int i = 2 / 0;
System.out.println("try of ...test2");//该层捕获了异常,则在异常代码之后的代码将不被执行
return i;
} catch (Exception e) {
System.out.println("catch of ...test2");
e.printStackTrace();
return 0;
} finally {
System.out.println("finally of ...test2");
}
}
}
输出:
catch of ...test2
finally of ...test2
try of ...test1
finally of ...test1
import java.io.File;
import java.io.FileNotFoundException;
public class TestException {
public static void main(String[] args) {
new AA().test1();
}
}
class AA {
public int test1(){
try {
//该层捕获了异常,则在异常代码之后的代码将不被执行
int j= new AA().test2();
System.out.println("try of ...test1");
return j;
} catch (Exception e) {
System.out.println("catch of ...test1");
e.printStackTrace();
return 0;
} finally {
System.out.println("finally of ...test1");
}
}
public int test2() {
try {
int i = 2 / 0;
System.out.println("try of ...test2");
return i;
} /*catch (Exception e) {
System.out.println("catch of ...test2");
e.printStackTrace();
return 0;
}*/ finally {
System.out.println("finally of ...test2");
}
}
}
输出:
finally of ...test2
catch of ...test1
finally of ...test1
分享到:
相关推荐
try-catch-finally捕获异常try-catch-finally捕获异常
使用try-catch-finally处理异常
异常+异常处理+try-catch-finally+教程 - 本资源是一个异常处理的教程,教你如何用Java的try-catch-finally语句处理异常,包括抛出和捕获异常,自定义异常,多个异常,finally块等。
try-catch-finally.js 843字节库,可在JavaScript中更灵活地捕获错误。内容注意事项按名称捕获可能不起作用按类型捕获在跨框架/过程中不起作用错误被消耗测验 安装 浏览器< script src =" try-catch-finally.js ...
The production IfStatement : if ( Expression ) Statement else Statement is evaluated as follows:.
:rocket: 不试 :rocket: 删除那些难看的try-catch-finally块,清理代码库! :smiling_face_with_heart-eyes: 关于在可能期望方法throw的代码库中工作可能会导致逻辑包裹在try-catch块中的情况。 它还导致其他代码...
Java的 try 与 catch finally关键字的使用
学习Java异常必要了解try_catch_finally的说明和使用讲解注意事项
try块中包含可能会产生异常的代码,当try块内的代码发生异常时,程序...另外,finally块是可选的,它会在try-catch块执行完毕后始终被执行,无论是否发生异常。finally块通常用于释放资源、关闭文件等必要的清理工作。
try-catch-finally执行顺序验证(左边是.java文件,右边是.class文件) 提示: try、catch块内的return操作编译后会变成把return的值保存到变量var的操作。 总结: try、catch块内的return操作编译后会变成把return的值...
压缩包里有本人录制的一个小视屏,不会的亲们,可以看下哦~~~ 笨人要期末考了呢,录些小视屏供自己复习所用,也是为了资源共享~O(∩_∩)O~
Condition 3: try中有异常,try-catch-finally里都没有return ,finally 之后有个returntry中有异常以后,根据
我们知道return语句用在某一个方法中,一是用于返回函数的执行结果,二是用于返回值为void类型的函数中,仅仅是一个return语句(return ;),此时用于结束方法的执行,也即此return后的语句将不会被执行,当然,这种...
第二:finally里面不建议放return语句,根据需要,return语句可以放在try和catch里面和函数的后。可行的做法有四: 1、return语句只在函数后出现一次。 2、return语句仅在try和catch里面都出现。 3、...
17.javatry…catch…finally语句.zip17.javatry…catch…finally语句.zip17.javatry…catch…finally语句.zip17.javatry…catch…finally语句.zip17.javatry…catch…finally语句.zip17.javatry…catch…finally语句...
Java的异常处理机制主要包括try-catch-finally语句和throw语句。 ### try-catch-finally语句 try-catch-finally语句是Java中处理异常的主要方式。try块包含可能会抛出异常的代码,catch块包含用于处理特定类型异常...
.Net 中基本的异常捕获与处理机制是由try…catch…finally块来完成的,它们分别完成了异常的监测、捕获与处理工作。一个try块可以对应零个或多个catch块,可以对应零个或一个finally块。不过没有catch的try似乎
如何使用 try-with-resources 代替try-catch-finally? I/O 什么是序列化?什么是反序列化? Java 序列化中如果有些字段不想进行序列化,怎么办? 获取用键盘输入常用的两种方法 Java 中 IO 流分为几种? 既然有了字节...
下面小编就为大家带来一篇java中使用try-catch-finally一些值得注意的事(必看)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧