Java异常捕获之一道try-catch-finally语句题
今天,学习了try-catch-finally语句,本来觉得蛮简单、易懂的。搜了一道相关类型的题。结果信心被泼了盆冷水。先把题Mark一下,出去透透风。
public class TestEx {
private int c;
public TestEx() {
}
@SuppressWarnings("finally")
boolean testEx() throws Exception {
boolean ret = true;
try {
ret = testEx1();
} catch (Exception e) {
System.out.println("testEx, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx, finally; return value=" + ret);
return ret;
}
}
@SuppressWarnings("finally")
boolean testEx1() throws Exception {
boolean ret = true;
try {
ret = testEx2();
if (!ret) {
return false;
}
System.out.println("testEx1, at the end of try");
return ret;
} catch (Exception e) {
System.out.println("testEx1, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx1, finally; return value=" + ret);
return ret;
}
}
@SuppressWarnings("finally")
boolean testEx2() throws Exception {
boolean ret = true;
try {
int b = 12;
for (int i = 2; i >= -2; i--) {
setC(b / i);
System.out.println("i=" + i);
}
return true;
} catch (Exception e) {
System.out.println("testEx2, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx2, finally; return value=" + ret);
return ret;
}
}
public static void main(String[] args) {
TestEx testException1 = new TestEx();
try {
testException1.testEx();
} catch (Exception e) {
e.printStackTrace();
}
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
}
Output:

看完之后我就在想,
1.Java异常处理机制,我真的理解了吗?
2.Java异常处理,我真的掌握了吗?
3.catch体里遇到return 是怎么处理?
4.finally 体里有return怎么处理?
5. catch 和 finally 体里同时遇上 return 怎么办?
6.是不是还有个System.exit()?遇到它又咋办??
7.仅仅知道throws不是完全之策啊,还要继续深入理解啊。2017-06-1918:21:08
Java异常捕获之一道try-catch-finally语句题的更多相关文章
- “全栈2019”Java异常第四章:catch代码块作用域详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- “全栈2019”Java异常第二十二章:try-with-resources语句详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- java异常捕获的一点感悟
class Annoyance extends Exception {} class Sneeze extends Annoyance {} class Human { public static v ...
- java 异常捕获小记
java 中异常捕获常用的为: try{ //业务代码 }catch(Exception e){ //异常捕获 }finally{ // 不管有无异常, 最后都会执行到这里 } 在方法体内如果想要把异 ...
- java异常捕获
类ExampleA继承Exception,类ExampleB继承ExampleA. 有如下代码片断: try { throw new ExampleB("b") } catch(E ...
- C# 异常捕获机制(Try Catch Finally)
一.C#的异常处理所用到关键字 try 用于检查发生的异常,并帮助发送任何可能的异常. catch 以控制权更大的方式处理错误,可以有多个catch子句. finally 无论是否引发了异常,fina ...
- Java异常捕获之try-catch-finally-return的执行顺序-转载
情况1:try块中没有抛出异常try和finally块中都有return语句 public static int NoException(){ int i=10; try{ System.out.pr ...
- C#中的异常捕获机制(try catch finally)
(转自:http://blog.csdn.net/zevin/article/details/6901489) 一.C#的异常处理所用到关键字try 用于检查发生的异常,并帮助发送任何可能的异常.ca ...
- java 异常捕获与异常处理
try{会产生异常的代码 }catch(出现异常的类型 e){ 异常出现后处理的方法 } 一旦异常出现又没有异常处理,程序就会中断. public static void main(String[] ...
随机推荐
- ionic 2.x 3.x 打包 压缩
大家都知道Ionic项目ionic serve生成的js css 非常庞大,小有1m多,大有几m,文件如此大load页面的时候需要较长时间的加载,特别在生产环境中,灰常不利于用户体验. 因此我们需要进 ...
- Python中super()的用法
参考链接:https://www.cnblogs.com/shengulong/p/7892266.html super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是 ...
- 04_python_列表
一.列表 列表是用[ ]括起来并每个元素用逗号分割的,并且可以存放各种数据类型,存放的数据量非常大,列表是有序的(按照你保存的顺序),有索引, 可以切片方便取值. lst = [1, '哈哈', &q ...
- " XSS易容术---bypass之编码混淆篇+辅助脚本编写"
一.前言本文原创作者:vk,本文属i春秋原创奖励计划,未经许可禁止转载!很多人对于XSS的了解不深.一提起来就是:“哦,弹窗的”.”哦,偷cookie的.”骚年,你根本不知道什么是力量.虽然我也不知道 ...
- yum 安装mysql数据库
1.先查看是否有安装mysql,有的话通过yum remove mysql先卸载掉,卸载完成后执行 yum install -y mysql-server mysql mysql-deve 2.启动m ...
- vue-cli初始化一个项目
1,换成淘宝源: npm config set registry https://registry.npm.taobao.org/ 检查是否修改成功 npm config get registry 2 ...
- linux源码安装的步骤
源码安装的过程中多多少少会遇到问题,在此仅简述一下安装的步骤,具体安装的过程中遇到的问题,具体解决. 安装步骤: 1.获取源码 name.gz 2.解包 tar -xvf name.gz (cd到包解 ...
- 一步步Cobol 400 上手自学入门教程03 - 数据部
数据部的作用 程序中涉及到的全部数据(输入.输出.中间)都要在此定义,对它们的属性进行说明.主要描述以下属性: 数据类型(数值/字符)和存储形式(长度) 数据项之间的关系(层次和层号) 文件与记录的关 ...
- JS常用工具函数(持续记录)
1.设置获取cookie //方式1 //设置cookie function SetCookie(name, value)//两个参数,一个是cookie的名字,一个是值 { var Days = 3 ...
- vue仿微信网页版|vue+web端聊天室|仿微信客户端vue版
一.项目介绍 基于Vue2.5.6+Vuex+vue-cli+vue-router+vue-gemini-scrollbar+swiper+elementUI等技术混合架构开发的仿微信web端聊天室— ...