天天看点

java动手动脑——异常处理

                                          Java07异常处理动手动脑

  1. 异常处理的基本知识

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

 try{

  程序代码

 }catch(异常类型1 异常的变量名1){

 }catch(异常类型2 异常的变量名2){

 }finally{

 }

2. 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

public class CatchWho2 {

        try {

               catch(ArithmeticException e) {

                   System.out.println(

"ArrayIndexOutOfBoundsException" + "/内层try-catch");

            System.out.println("发生ArithmeticException");

            System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");

运行结果:ArrayIndexOutOfBoundsException/外层try-catch

3. 阅读

EmbedFinally.java

示例,再运行它,观察其输出并进行总结。

多个catch块时候,Java虚拟机会匹配其中一个异常类或其子类,就执行这个catch块,而不会再执行别的catch块。finally语句在任何情况下都必须执行的代码,这样可以保证一些在任何情况下都必须执行代码的可靠性。

4. finally语句块一定会执行吗?

并不一定,如果提前结束程序,finally语句就不会被执行,如程序中用System.exit(0);提前结束了程序。

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

import

java.util.*;

class AException

extends Exception

{

String

a;

AException() 

a="输入有误";  

public

String toString()

return a;

public class A

{

    public static void main(String args[])

    {

     while(1>0)

       {

     Scanner sc = new Scanner(System.in);

     System.out.println("请输入考试成绩(0~100):");

        try

        {

            String s = sc.nextLine();    

            getnum(s);

        catch (AException e)

            System.out.println(e.toString());

       }

    private static void getnum(String s) throws AException

     for (int i = s.length()-1; i >= 0;i--)

        {

         int chr = s.charAt(i);

            if (chr < 48 || chr > 57)

            {

             throw new AException();

            }

        } 

     double num = Double.parseDouble(s);  

        if (num < 0 || num> 100)

            throw new AException();

        if (num>= 0 && num<= 60)

            System.out.print("不及格\n");

        else if (num >= 60 && num <= 70)

            System.out.print("及格\n");

        else if (num>= 70 && num<= 80)

            System.out.print("中\n");

        }

        else if (num >= 80 && num <= 90)

            System.out.print("良\n");

        else

            System.out.print("优\n");