try catch finally的一些用法
一:例题:
package test; import javax.swing.*; class AboutException {
public static void main(String[] a)
{
int i=1, j=0, k;
try
{ k = i/j; // Causes division-by-zero exception
//throw new Exception("Hello.Exception!");
} catch ( ArithmeticException e)
{
System.out.println("被0除. "+ e.getMessage());
}
finally
{
JOptionPane.showConfirmDialog(null,"OK");
} }
}
其结果显示
被0除. / by zero
总结:程序看下来,1/0在数学逻辑上是错误的,在计算机运行中肯定会报错,当编程过程中,有些地方不太能更彻底的了解漏洞在哪里,那么try和catch机制来最大化地挽留损失,以上程序中,try将可能发生错误的地方抛出,当执行发生错误了,计算机并不报错,而是有catch语句抓住,执行其中内容,后面还有个finally语句,用于善后的代码,不管是否有异常发生,finally语句块中的语句始终保证被执行.
二:例题:
package test; public class CatchWho {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
} throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}
结果:
ArrayIndexOutOfBoundsException/内层try-catch
发生ArithmeticException
分析:
注意其结构,里面的try抛出异常一,对应的catch(紧跟着的catch)接收一并执行,外层的try抛出异常二,对应的catch接收二并执行,抛出异常和接收异常这执行可以看成是一个单一的动作操作,最后的catch就没有抛出这一动作执行,所以不运行(因为抛出异常一已被接住)。
看另一个例题:
package test; public class CatchWho2 {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArithmeticException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
}
throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}
其结果为:
ArrayIndexOutOfBoundsException/外层try-catch
分析:
此例题和上面例题的区别就在于,里面的catch所接住的方向变了。
按着程序顺序分析下来,当里面的try抛出异常时,只有外面的catch能接住,那么开始执行外面的catch,顺序就从刚才执行的语句之下执行下去了,即便是外面的catch交换顺序,结果一样不变。
由两题总结:try catch这一模式,是有顺序依据的,当执行try语句是,紧接着的就是所对应的catch来执行,然后接着catch继续执行下去。
三:例题:
package test; 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");
}
}
}
结果:
in Level 1
in Level 2
in Level 3
Level 3:class java.lang.ArithmeticException
In Level 3 finally
In Level 2 finally
In Level 1 finally
总结:finally是无论是否出现异常都会执行的,在第三个try中出现异常,紧跟着的catch已经接收到,但此并不算第二个try出现异常,因为异常已经解决,那么之后就不会显示第二个和第一个catch的内容了。我将内容改了一下,就验证了我的说法。
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 (ArrayIndexOutOfBoundsException e) { //我让此catch不能捕捉到上面的try,有意让第二个catch抓到
System.out.println("Level 3:" + e.getClass().toString());
}
finally {
System.out.println("In Level 3 finally");
}
// result=100/0; //Level 2
}
catch (Exception e) {//第二个catch
System.out.println("Level 2:" + e.getClass().toString());
}
finally {
System.out.println("In Level 2 finally");
}
结果显示
in Level 1
in Level 2
in Level 3
In Level 3 finally
Level 2:class java.lang.ArithmeticException
In Level 2 finally
In Level 1 finally
结论:try catch着一单一动作(前提是配套),就是出现异常和处理异常的程序,那总的来看,依旧是无异常的代码。
四:例题:
package test; public class SystemExitAndFinally { public static void main(String[] args)
{
try{ System.out.println("in main");
throw new Exception("Exception is thrown in main");
//System.exit(0); }
catch(Exception e)
{
System.out.println(e.getMessage());
System.exit(0);
}
finally
{
System.out.println("in finally");
}
} }
结果:
in main
Exception is thrown in main
总结:
JVM是java虚拟机,finally是由JVM保证执行,而System.exit(0)是正常退出程序,结束JVM的运行,那么最后finally就不再执行。
PS:System.exit(status)不管status为何值都会退出程序,非0,则表示为非正常退出程序,一般放在catch块中,当捕获到异常,需要停止程序,用非0值来表示非正常退出程序
try catch finally的一些用法的更多相关文章
- 【Java学习笔记之三十三】详解Java中try,catch,finally的用法及分析
这一篇我们将会介绍java中try,catch,finally的用法 以下先给出try,catch用法: try { //需要被检测的异常代码 } catch(Exception e) { //异常处 ...
- C++异常处理:try,catch,throw,finally的用法
写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...
- C++异常处理: try,catch,throw,finally的用法
写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...
- try,catch捕获错误的用法
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <script&g ...
- 关于错误处理程序中【return】的用法
先让俺这位新人帮各位有幸游览到我博客文章的叔叔阿姨哥哥姐姐们解释一下什么是错误处理?即:当程序发生错误时,保证程序不会异常中断的机制. 那么为什么程序中会有错误处理呢?像我们通常无论是玩手机或者玩游戏 ...
- 细嗅Promise
读完这篇文章,预计会消耗你 40 分钟的时间. Ajax 出现的时候,刮来了一阵异步之风,现在 Nodejs 火爆,又一阵异步狂风刮了过来.需求是越来越苛刻,用户对性能的要求也是越来越高,随之而来的是 ...
- ajaxfileupload asp.net 的简单使用
本人菜鸟,第一次写博客,不会排版,只是记录工作中常用的东西 ajaxfileupload.js源码: http://www.rczjp.cn/HTML/110420/20113620053635.ht ...
- 兼容ie的jquery ajax文件上传
Ajax文件上传插件很多,但兼容性各不一样,许多是对ie不兼容的,另外项目中是要求将网页内容嵌入到桌面端应用的,这样就不允许带flash的上传插件了,如:jquery uploadify...悲剧 对 ...
- WebView自适应并嵌套在ScrollView里
大致思路:通过流的形式把网页抓取下来,然后对webView进行设置. 1.对webView进行设置 web.setWebViewClient(new WebViewClient() { @Overri ...
随机推荐
- linux 解压缩
tar f 使用档案名字,这个参数是最后一个参数,后面只能接档案名 c 建立压缩档案 x 解压 t 查看内容 r 向压缩归档文件末尾追加文件 u 更新原压缩包中的文件 z 有gzip属性的 j 有bz ...
- jquery after append appendTo三个函数的区别
jq文档的说明是 1.after函数 定义和用法: after() 方法在被选元素后插入指定的内容. 语法: $(selector).after(content) 实例: <html> & ...
- 自定义ToolBar之一
其实已经有很多大神写过这方面的文章了,不过我比较蠢吧,老有一些地方看不懂的,翻了很多关于Toolbar方面的文章和视频,这儿总结一下. 参考资料:youtube:slidenerd 阶段一 自定义配 ...
- Gson简要使用笔记
最近在做一个java web service项目,需要用到jason,本人对java不是特别精通,于是开始搜索一些java平台的json类库. 发现了google的gson,因为之前对于protoco ...
- Spring自动装配与扫描注解
1 javabean的自动装配 自动注入,减少xml文件的配置信息. <?xml version="1.0" encoding="UTF-8"?> ...
- 模拟赛1102d2
/* φ(n)=φ(p^k)=p^k-p^(k-1)=(p-1)*p^(k-1) φ(m*n)=φ(m)*φ(n) 直接套公式做,因为分解质因数时,只分解一个数,所以可以不打素数表,只将n分解到√n就 ...
- c语言运算符
一.op=形式的赋值操作符 int a=0; a+=1; //等价于 a=a+1;// a*=1; 二.op=类表达式 int a=10,b=5; a/=b;//等价于a=a/b; a*=b+1; ...
- elipse插件整理
整理一下用过的eclipse插件: 1. WindowBuilder :swing插件,可以拖啊拖啊拖出来一个窗口,可以显著提高开发效率. 官网: http://www.eclipse.org/w ...
- .NET开发工具之Excel导出公共类
来源:Pino晨 链接:cnblogs.com/chenxygx/p/5954870.html 说明 最近接了一个任务,就是做一个列表的Excel导出功能.并且有很多页面都会使用这个功能. 导出的Ex ...
- Linux常用的日志分析命令与工具
>>基础命令 操作 命令 说明 查看文件的内容 cat -n access.log -n显示行号 分页显示文件 more access.log Enter下一行,空格下一页,F下一屏,B上 ...