09课堂问题整理

作业归档9 异常处理

第一部分:完成课件中的动手动脑的或需要验证的相关内容。

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

第一部分

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

源代码:

package demo;

import javax.swing.*;

class AboutException {

    public static void main(String[] a)

    {

        int i=1, j=0, k;

        k=i/j;

        //double 类型可以被0

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

        }

  }

}

总结:

try中写可能发生运行错误的代码,catch(异常类型 异常对象引用)中写用于处理异常的代码,finally中写用于“善后”的代码。

java 中的可捕获异常的类都派生自 Exception 类。

当异常发生时,程序控制流程由try语句块跳转到catch语句块。

不论异常是否发生,finally语句块中的语句始终保证被执行。

 

当数据类型为double类型时,可以被0除,结果是无穷大。

2、阅读以下代码(CatchWho.java),写出程序运行结果。

源代码:

package demo;

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

3、写出CatchWho2.java程序运行的结果

源代码:

package demo;

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

总结:

必须是一扔一接。

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

源代码:

package demo;

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

②当第一处注释去掉

in Level 1

in Level 2

Level 2:class java.lang.ArithmeticException

In Level 2 finally

In Level 1 finally

总结:

当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,会导致不同的finally语句块执行顺序。要注意层序。

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

源程序:

package demo;

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

总结:

Finally语句块不一定被执行,当程序遇到System.exit(0);结束后,后面的Finally语句块不会被执行。

第二部分

源代码:

import java.util.Scanner;

public class TestScore {

    public static void main(String [] args){

     ScoreException err = new ScoreException("请输入正确格式:");

     ScoreException1 err1 = new ScoreException1("请输入正确分数:");

       

     Scanner sc=new Scanner(System.in);

     System.out.println("请输入成绩:");

     String score = null;

     try{

     try{

         score=sc.next();

         try{

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

             char ch=score.charAt(i);

             if((ch<'0'||ch>'9')&&ch!='.'){

             throw err;

             }

             }

             }

         catch(ScoreException e){

         System.out.println("格式错误!");

         System.exit(0);

         }

        

         double score1=Double.parseDouble(score);

         if(score1<0||score1>100){

         throw err1;

         }

         }

         catch(ScoreException1 e){

         System.out.println("分数不在范围内!");

         System.exit(0);

         }

        

         double score2=Double.parseDouble(score);

         if(score2>=90)

         System.out.println("等级:优秀");

         else if(score2>=80)

         System.out.println("等级:良好");

         else if(score2>=70)

         System.out.println("等级:中等");

         else if(score2>=60)

         System.out.println("等级:及格");

         else

         System.out.println("等级:不及格");

     }

     finally{}

    }

}

class ScoreException extends Exception{

public ScoreException(String msg){

super(msg);

}

}

class ScoreException1 extends Exception{

public ScoreException1(String msg){

super(msg);

}

}

运行结果:

请输入成绩:

bhbdbhv

格式错误!

请输入成绩:

9654

分数不在范围内!

请输入成绩:

98.6

等级:优秀

 

原文地址:https://www.cnblogs.com/hjy415/p/6099837.html