您的位置:首页 > 其它

try catch finally的一些用法

2015-11-15 11:20 381 查看
一:例题:

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值来表示非正常退出程序
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: