catch的执行与try的匹配
这里有一段代码:
public class EmbededFinally {
public static void main(String args[]) {
int result;
try {
System.out.println("in Level 1");
try {
System.out.println("in Level 2");
// result=100/0; //Level 2
try {
System.out.println("in Level 3");
result=100/0; //Level 3
}
catch (Exception e) {
System.out.println("Level 3:" + e.getClass().toString());
}
finally {
System.out.println("In Level 3 finally");
}
// result=100/0; //Level 2
}
catch (Exception e) {
System.out.println("Level 2:" + e.getClass().toString());
}
finally {
System.out.println("In Level 2 finally");
}
// result = 100 / 0; //level 1
}
catch (Exception e) {
System.out.println("Level 1:" + e.getClass().toString());
}
finally {
System.out.println("In Level 1 finally");
}
}
}
当我去掉level2中顶部的在level3 try之前的注释时level3中的catch不执行。
当我去掉level2中底部的在level3之后的注释时level3中的catch执行。
于是得到,当在try内发现错误时则抛出异常,而try内编码则不继续向下执行。之后catch接到异常并执行,最后执行同等级finally。
catch的执行与try的匹配的更多相关文章
- 异常处理 try...catch...finally 执行顺序, 以及对返回值得影响
异常处理 try...catch...finally 执行顺序, 以及对返回值得影响 结论:1.不管有没有出现异常,finally块中代码都会执行:2.当try和catch中有return时,fina ...
- Java中try catch finally执行
直接上代码实例: public static void main(String[] args) { System.out.println(test1()); } static int test1 ...
- try catch finally执行顺序
1.不管有木有出现异常,finally块中代码都会执行: 2.当try和catch中有return时,finally仍然会执行: 3.finally是在return表达式运算后前执行的,所以函数返回值 ...
- C# try catch finally 执行
try { //dosomething eg: int a = 1; int b = 2; int c = a + b; if(c>2) { return; } } catch(Exceptio ...
- try..catch..finally执行顺序return
try..catch..finally这个语法大家都很熟悉,就是捕捉异常.处理异常,面试中经常被问到的一个问题是:如果在try...catch中的某某地方return了,那么之后的某某步骤还会不会执行 ...
- try catch finally 执行顺序面试题总结
在网上看到一些异常处理的面试题,试着总结一下,先看下面代码,把这个方法在main中进行调用打印返回结果,看看结果输出什么. public static int testBasic(){ int i = ...
- 关于try catch块执行流程
代码: package test; public class FinallyTest { public static void main(String[] args) { try { // proce ...
- JAVA代码中加了Try...Catch的执行顺序
public static String getString(){ try { //return "a" + 1/0; return "a"; } catch ...
- try catch finally执行顺序 (return / 变量覆盖)
finally有return 始终返回finally中的return 抛弃 try 与catch中的return 情况1:try{} catch(){}finally{} return x; try{ ...
随机推荐
- Calendar类中add/set/roll方法的区别
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- ing
#include <stdio.h> int main(){ int a,b; while(scanf("%d%d",&a,&b)!=EOF & ...
- 任性,新建对象不用new
先看最简单的一个例子: window.meng = window.meng || {}; (function () { /** * * @param {Number}width * @param {N ...
- VBA中四种自动运行的宏以及模块的含义
在Excel的“标准模块”中可以创建4种自动运行的宏,它们分别是Auto_Open(打开工作 簿时自动运行), Auto_Close, Auto_Activate, Auto_Deactivate. ...
- poj-------------(2752)Seek the Name, Seek the Fame(kmp)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11831 Ac ...
- C#常用的加密算法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 方法一: //须添加对System.Web的引用 ...
- BZOJ1737 [Usaco2005 jan]Naptime 午睡时间
断环然后裸DP就好了... $f[i][j][k]$表示1号时间段没有被算入答案,到了第$i$个时间段,一共选了$j$个时间段,$k = 0 /1$表示第i个时间段有没有被算进答案的最优值 $g[i] ...
- 理解squid的正向和反向代理
1.相同点: 访问的走向都是:客户端 -> 代理服务器 ->真实服务器 ->代理服务器->客户端 2.不同点:正向代理语义上更侧重于,让代理服务器去帮忙请求某个网址.让代理服务 ...
- HDU 2795 单点更新,区间优先查找(想法)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- CSS 垂直居中。
1,display: table; display: table-cell <div style="border:solid red 1px ;height:200px;width:2 ...