zoukankan      html  css  js  c++  java
  • 09-异常处理

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

    程序源代码:

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

    运行结果截图:

    【作业】请尝试解释以下奇怪的现象!

    解释:

    【作业】阅读以下代码(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");
            }
        }
    }

    运行结果截图:

    分析:

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

    【作业】写出CatchWho2.java程序运行的结果

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

    运行结果截图:

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

    【作业】请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。


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

    }

    运行结果截图:

    finally是无论是否出现异常都会执行的,在第三个try中出现异常,紧跟着的catch已经接收到,但此并不算第二个try出现异常,因为异常已经解决,那么之后就不会显示第二个和第一个catch的内容了

    【作业】辨析:finally语句块一定会执行吗?
    请通过 SystemExitAndFinally.java示例程序回答上述问题


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


    }

    运行结果截图:

    总结:

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

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

    【作业】编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
    要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。

    import java.util.Scanner;

    public class Mark{
     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("不及格");
       }
       if(score>=60&&score<=69)
       {
        System.out.println("及格");
       }
       if(score>=70&&score<=79)
       {
        System.out.println("中");
       }
       if(score>=80&&score<=89)
       {
        System.out.println("良");
       }
       if(score>=90&&score<=100)
       {
        System.out.println("优");
       }
       if(score<0||score>100)
       {
        System.out.println("输入的数字过大或过小");
       }
     else
     {
      System.out.println("");
     }
      }
      catch(Exception e)
      {
       System.out.println("请输入正确数字:");
      }
     }
    }

    运行结果截图:

  • 相关阅读:
    ZOJ 3556
    ZOJ 2836
    HDU 2841
    HDU 4135
    POJ 3695
    POJ 2773
    HDU 4407
    HDU 1796
    ZOJ 3688
    ZOJ 3687
  • 原文地址:https://www.cnblogs.com/kangy123/p/6094916.html
Copyright © 2011-2022 走看看