异常处理

阅读以下代码(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

 

 

写出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

 

throw语句用在方法体内,表示抛出异常,由方法体内的语句处理
throws语句用在方法声明后面,表示再抛出异常,由调用这个方法的上一级方法中的语句来处理
throws主要是声明这个方法会抛出这种类型的异常,使其他地方调用它时知道要捕获这个异常。
throw是具体向外抛异常的动作,所以它是抛出一个异常实例。
throws说明你有哪个可能,倾向
throw的话,那就是你把那个倾向变成真实的了
同时:
1)throws出现在方法函数头;而throw出现在函数体;
2)throws表示出现异常的一种可能性,并不一定会发生这些异常;throw则是抛出了异常,执行throw则一定抛出了某种异常;
3)两者都是消极处理异常的方式(这里的消极并不是说这种方式不好),只是抛出或者可能抛出异常,但是不会由函数去处理异常,真正的处理异常由函数的上层调用处理。

 

Java异常处理主要通过5个关键字控制:try、catch、throw、throws和finally。try的意思是试试它所包含的代码段中是否会发生异常;而catch当有异常时抓住它,并进行相应的处理,使程序不受异常的影响而继续执行下去;throw是在程序中明确引发异常;throws的作用是如果一个方法可以引发异常,而它本身并不对该异常处理,那么它必须将这个异常抛给调用它的方法;finally是无论发不发生异常都要被执行的代码

 

 

当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机。

请先阅读 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        //由于result是整形的,故除于0结果会出现错误

                   

                }

               

                catch (Exception e) {

                   

                    System.out.println("Level 3:" + e.getClass().toString());    //getClass是返回运行时的类,返回算术异常

               

                }

                

               

                finally {

                   

                    System.out.println("In Level 3 finally");

               

                }

               

              

                // result=100/0;  //Level 2

 

           

                }

           

            catch (Exception e) {

               

                System.out.println("Level 2:" + e.getClass().toString());      //如果Level2出现错误,则把错误信息输出

          

            }

            finally {

               

                System.out.println("In Level 2 finally");

          

             }

            

            // result = 100 / 0;  //level 1

       

        }

       

        catch (Exception e) {

           

            System.out.println("Level 1:" + e.getClass().toString());     //如果Level1出现错误,则把错误信息输出   

       

        }

       

        finally {

          

            System.out.println("In Level 1 finally");

       

        }

   

    }

 

}

 

结果截图:

 

 

总结:try的意思是试试它所包含的代码段中是否会发生异常;而catch当有异常时抓住它,并进行相应的处理,使程序不受异常的影响而继续执行下去;throw是在程序中明确引发异常;finally是无论如何都执行程序。

 

 

辨析: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");

       

        }

   

    }

 

}

 

结论:通过上述程序我知道了finally为何没有执行的原因,当在try里面throw一个Exception的时候,程序将不在执行finally

 

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

要求程序必须具备足够的健壮性,不管用户输入什     么样的内容,都不会崩溃。

源代码

package demo;

import javax.swing.JOptionPane;

public class Test {

       public static void  main(String[] args)

       {

              double m=0;

              for(int i=0;i<1000000;i++)

              {

                     String input=JOptionPane.showInputDialog("请输入学生成绩"); 

                     try

                     {

                            m=Double.valueOf(input);

                            if(m<0)

                            {

                                   JOptionPane.showMessageDialog(null,"输入有误,请再次输入");

                            }

                            else if(m<60)

                            {

                                   JOptionPane.showMessageDialog(null,"不及格");

                            }

                           

                            else if(m<70)

                            {

                                   JOptionPane.showMessageDialog(null,"及格");

                            }

                           

                            else if(m<80)

                            {

                                   JOptionPane.showMessageDialog(null,"中");

                            }

                           

                            else if(m<90)

                            {

                                   JOptionPane.showMessageDialog(null,"良");

                            }

                           

                            else  if(m<=100)

                            {

                                   JOptionPane.showMessageDialog(null,"优");

                            }

                           

                            else if(m>100)

                            {

                                   JOptionPane.showMessageDialog(null,"输入有误");

                            }

                     }

                    

                     catch(NumberFormatException e)

                     {

                            JOptionPane.showMessageDialog(null,"输入有误");

                     }

              }

             

       }

 

}

 

 

结果截图

 

 

 

posted on 2016-11-25 22:00  Doheart  阅读(117)  评论(0编辑  收藏  举报

导航