一:例题:

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的一些用法的更多相关文章

  1. 【Java学习笔记之三十三】详解Java中try,catch,finally的用法及分析

    这一篇我们将会介绍java中try,catch,finally的用法 以下先给出try,catch用法: try { //需要被检测的异常代码 } catch(Exception e) { //异常处 ...

  2. C++异常处理:try,catch,throw,finally的用法

    写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...

  3. C++异常处理: try,catch,throw,finally的用法

    写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...

  4. try,catch捕获错误的用法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <script&g ...

  5. 关于错误处理程序中【return】的用法

    先让俺这位新人帮各位有幸游览到我博客文章的叔叔阿姨哥哥姐姐们解释一下什么是错误处理?即:当程序发生错误时,保证程序不会异常中断的机制. 那么为什么程序中会有错误处理呢?像我们通常无论是玩手机或者玩游戏 ...

  6. 细嗅Promise

    读完这篇文章,预计会消耗你 40 分钟的时间. Ajax 出现的时候,刮来了一阵异步之风,现在 Nodejs 火爆,又一阵异步狂风刮了过来.需求是越来越苛刻,用户对性能的要求也是越来越高,随之而来的是 ...

  7. ajaxfileupload asp.net 的简单使用

    本人菜鸟,第一次写博客,不会排版,只是记录工作中常用的东西 ajaxfileupload.js源码: http://www.rczjp.cn/HTML/110420/20113620053635.ht ...

  8. 兼容ie的jquery ajax文件上传

    Ajax文件上传插件很多,但兼容性各不一样,许多是对ie不兼容的,另外项目中是要求将网页内容嵌入到桌面端应用的,这样就不允许带flash的上传插件了,如:jquery uploadify...悲剧 对 ...

  9. WebView自适应并嵌套在ScrollView里

    大致思路:通过流的形式把网页抓取下来,然后对webView进行设置. 1.对webView进行设置 web.setWebViewClient(new WebViewClient() { @Overri ...

随机推荐

  1. linux 共享内存实现

    说起共享内存,一般来说会让人想起下面一些方法:1.多线程.线程之间的内存都是共享的.更确切的说,属于同一进程的线程使用的是同一个地址空间,而不是在不同地址空间之间进行内存共享:2.父子进程间的内存共享 ...

  2. C# 串口操作系列(2) -- 入门篇,为什么我的串口程序在关闭串口时候会死锁 ?

    第一篇文章我相信很多人不看都能做的出来,但是,用过微软SerialPort类的人,都遇到过这个尴尬,关闭串口的时候会让软件死锁.天哪,我可不是武断,算了.不要太绝对了.99.9%的人吧,都遇到过这个问 ...

  3. 警告:Assigning to 'id<Delegate>' from incompatible type 'ViewController *const_st

    原因: 你自己写了代理,设置了   delegate = self.但是self 没有遵守这个协议 只需要遵守这个协议就可以消除警告.

  4. .Net的DataGrid的使用

    先上图吧

  5. Java_内存管理String and Array

    题目1.指出下列程序运行的结果 ()public class Example { String str = new String("good"); char[] ch = { 'a ...

  6. python基础——偏函数

    python基础——偏函数 Python的functools模块提供了很多有用的功能,其中一个就是偏函数(Partial function).要注意,这里的偏函数和数学意义上的偏函数不一样. 在介绍函 ...

  7. file标签选择文件change事件失效处理方法

    file只能处罚一次change事件,在change事件中重新替换file标签即可生效 eg: $(function(){ //上传图片 $("body").on("ch ...

  8. 登录到mysql查看binlog日志

    查看当前第一个binlog文件的内容 show binlog events; 查看指定binlog文件内容 show binlog events in 'mysql-bin.000002'; 查看当前 ...

  9. C#集合类型

    using System; using System.Collections; using System.Collections.Generic; namespace codeTest { class ...

  10. 【Javascript】IE8兼容 背景图片与a标签的onclick事件

    先说几句牢骚话. 虽然IE8比之IE6.7有很大的进步,但是在执行效率.兼容性上仍然有很多问题.被广大开发者喜爱的平台才是好平台. 可惜多亏当年盗版XP打开中国的计算机市场,IE作为一款捆绑软件仍然在 ...