码迷,mamicode.com
首页 > 其他好文 > 详细

异常处理动手动脑及课后练习

时间:2017-11-17 00:02:38      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:.com   位置   util   ceo   执行顺序   string   trace   visio   class   

1.

.技术分享

 

代码:

import javax.swing.*;

public class AboutException {
public static void main(String[] a)
{
int i=1, j=0, k;
k=i/j;


try
{

k = i/j; // Causes division-by-zero exception
//throw new Exception("Hello.Exception!");
}

catch ( ArithmeticException e)
{
System.out.println("被0除. "+ e.getMessage());
}

catch (Exception e)
{
if (e instanceof ArithmeticException)
System.out.println("被0除");
else
{
System.out.println(e.getMessage());

}
}


finally
{
JOptionPane.showConfirmDialog(null,"OK");
}

}
}

运行结果:

技术分享

2.

技术分享

 

 

解答:

反编译:

技术分享

技术分享

以下是PPT给出的参考:

技术分享

 

 

 生成的字节码指令不一样。

3.

 

技术分享

 

技术分享

 

里面的try抛出异常一,对应的catch(紧跟着的catch)接收一并执行,外层的try抛出异常二,对应的catch接收二并执行,抛出异常和接收异常这执行可以看成是一个单一的动作操作,最后的catch就没有抛出这一动作执行,所以不运行(因为抛出异常一已被接住)。

 

 4.

技术分享

代码:


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");

}

}

}运行结果:

技术分享

 

 

此例题和上面例题的区别就在于,里面的catch所接住的方向变了。
按着程序顺序分析下来,当里面的try抛出异常时,只有外面的catch能接住,那么开始执行外面的catch,顺序就从刚才执行的语句之下执行下去了,即便是外面的catch交换顺序,结果一样不变。当有多层嵌套的finally时,异常在不同的层次抛出,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。
总结:try catch这一模式,是有顺序依据的,当执行try语句是,紧接着的就是所对应的catch来执行,然后接着catch继续执行下去。

5.

技术分享

源代码:


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");
}
}
}运行结果:

技术分享

finally语句一定会执行吗?答案必然是否定的 ,上述运行已经给出了结果。why?

JVM是java虚拟机,finally是由JVM保证执行,而System.exit(0)是正常退出程序,结束JVM的运行,那么最后finally就不再执行。

finally语句不被执行的唯一情况是先执行了用于终止程序的System.exit()方法。

6.

技术分享

 

源代码:

// UsingExceptions.java
// Demonstrating the getMessage and printStackTrace
// methods inherited into all exception classes.
public class PrintExceptionStack {
public static void main( String args[] )
{
try {
method1();
}
catch ( Exception e ) {
System.err.println( e.getMessage() + "\n" );
e.printStackTrace();
}
}

public static void method1() throws Exception
{
method2();
}

public static void method2() throws Exception
{
method3();
}

public static void method3() throws Exception
{
throw new Exception( "Exception thrown in method3" );
}
}

 

技术分享

 

 

 

 7.依据对本讲多个示例程序的分析,请自行归纳总结出Java多层嵌套异常处理的基本流程。

 答:

1)在java语言中,通常将可能出现异常的语句放入try{}语句中,将出现错误后需要执行的语句放入到catch{}语句中,将无论是否发生异常都要执行的语句放在finally{}语句中。

2)当程序执行出现异常的时候,系统会抛出一个异常,然后由try{}语句中中出现异常的地方转到catch{}语句中。不过不管有没有异常产生,finally{}中的语句都将执行。

3)如果系统出现系统错误或者运行Runtime异常,jvm会结束程序运行,不一定会执行finally{}中的语句。

4)如果try{}中产生的异常在catch中没有处理,系统将停止程序,也不会执行finally中的语句。

基本过程是用try语句块包住要监视的语句,如果在try语句块内出现异常,则异常会被抛出,你的代码在catch语句块中可以捕获到这个异常并做处理;还有以部分系统生成的异常在Java运行时自动抛出。你也可以通过throws关键字在方法上声明该方法要抛出异常,然后在方法内部通过throw抛出异常对象。finally语句块会在方法执行return之前执行。

 

 8.

技术分享

 

代码:

import java.util.*;
public class PrintExceptionStack
{
public static void main(String[] args)
{
Scanner sc =new Scanner(System.in);
int score=0;
System.out.print("请输入成绩:");
try
{
score=sc.nextInt();
if(score>=0&&score<=59)
{
System.out.println("不及格");
}
else if(score>=60&&score<=69)
{
System.out.println("及格");
}
else if(score>=70&&score<=79)
{
System.out.println("中");
}
else if(score>=80&&score<=89)
{
System.out.println("良");
}
else if(score>=90&&score<=100)
{
System.out.println("优");
}
else
{
System.out.println("输入的数字过大或过小");
}
}
catch(Exception e)
{
System.out.println("输入错误,请输入正确数字!");
}
}
}

 运行结果:

技术分享技术分享技术分享技术分享技术分享技术分享

 

异常处理动手动脑及课后练习

标签:.com   位置   util   ceo   执行顺序   string   trace   visio   class   

原文地址:http://www.cnblogs.com/mm20/p/7846885.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!