java异常处理动手动脑

实验任务一、多层的异常捕获-1

1.实验内容:阅读以下代码(CatchWho.java),写出程序运行结果:

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

        }

    }

   

}

 

2.结果截图:

 

实验任务二、多层的异常捕获-2

1.实验内容:写出CatchWho2.java程序的运行结果

   public class CathchWho2{

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

        }

    }

}

 

 

2.结果截图:

 

实验任务三:动手动脑1

1.实验内容:请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识。

2.代码:

import javax.swing.*;

 

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

     }

             

  }

}

 

3.运行结果截图:

 

4.总结:将可能要出现错误的语句放到try语句中,当程序检测到这个错误时,用catch语句捕获并且处理该错误,如果发生了异常,程序会由try语句跳到catch语句。但如果没有提供合适的异常处理代码,JVM会结束整个应用程序。

实验任务四:动手动脑2

1.实验内容:请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。

  代码:

import javax.swing.*;

public class EmbedFinally{

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

       

              }

   

       }

}

 

 

2.运行结果截图:

 

 

3.总结:当try....catch....finally语句嵌套使用时,程序会先执行内层的try语句,当检测到错误时由catch语句捕获,而内层捕获并处理了异常,外层就不会再捕获,只执行finally语句块,如果内层并未处理该异常,则外层捕获并处理异常,执行外层的finally语句,比如将上述代码修改后的结果并不相同:

import javax.swing.*;

public class EmbedFinally{

       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

               

                            }

               

               

                            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语句捕获到异常,但仍执行finally语句块,由外层的catch语句捕获到异常并处理,执行该层的finally语句,再外层的catch便不再捕获该异常,只执行finally语句块。

实验任务五:动手动脑3

1.实验内容:辨析finally语句块一定会执行吗?请通过 SystemExitAndFinally.java示例程序回答上述问题

   2.代码:

import javax.swing.*;

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

       

              }

    }

}

3.结果截图:

 

4.分析:

如果上述代码修改为:

import javax.swing.*;

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

       

              }

       

              finally

       

              {

           

                     System.out.println("in finally");

       

              }

    }

}

而这段程序执行的结果为:

 

说明这段程序执行了finally语句块,一般情况下,finally语句块一定执行,但是当异常处理代码catch中执行System.exit(0)时,会退出java虚拟机,也就不再执行finally语句块。

实验任务六:归纳与总结

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

2.总结:先内层捕获异常,进行处理,处理完之后外层不再处理,如有finally语句则执行,若catch语句中执行了System.exit(0)时,finally语句也不再执行。如果内层未捕获异常,则由外层处理异常,然后执行finally语句块,同样,若catch语句执行了System.exit(0),finally语句不再执行。如果有多个catch语句,则如果有一个捕获到之后,其它的catch语句被忽略,不再执行。

实验任务七:动手动脑4

1.实验内容:编写一个程序,此程序在运行时要求用户输入一个整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。

2.代码:

package Tutorial9;

import java.util.*;

class yichang extends Exception{

      

}

class yichang1 extends Exception{

      

}

public class Zuoye {

       static Zuoye A=new Zuoye();

       public static void Dongnao(){

               Scanner in=new Scanner(System.in);

           System.out.print("请输入一个整数:");

           int t=1;

            int m1;

               String m=in.next();

               for(int i=0;i<m.length();i++){

                      if(m.charAt(i)<'0'||m.charAt(i)>'9')

                      {

                             try{

                                    throw new yichang();

                             }catch(yichang e){

                                    t=0;

                                    System.out.println("数字格式异常,重新输入");

                                    break;

                             }

                      }

               }

               if(t==1){

                      m1=Integer.parseInt(m);

                      if(m1>100){

                             try{

                                    throw new yichang1();

                             }catch(yichang1 e){

                                    System.out.println("不合法,重新输入");

                             }

                      }

                      else{

                          if(m1<60){

                                 System.out.println("不及格");

                          }

                          else if(m1>=60)

                          {

                                 if(m1>65&&m1<75){

                                        System.out.println("中");

                                 }

                                 else if(m1>=75&&m1<90){

                                        System.out.println("良");

                                 }

                                 else if(m1>=90&&m1<=100){

                                        System.out.println("优");

                                 }

                                 else{

                                        System.out.println("及格");

                                 }

                          }

                      }

               }

               else

                     A.Dongnao();

       }

     public static void main(String args[]){

           while(true){

            A.Dongnao();

           }

     }

}

3.结果截图:

 

posted @ 2016-11-24 22:00  Joker明哥  阅读(175)  评论(0编辑  收藏  举报