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示例,了解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语句,如果没有处理,结束整个程序。

 

实验任务四:动手动脑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语句捕获处理,出现浴场则由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.实验内容:验证 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.结果截图:

 

 

 

修改为:

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语句块一定执行,当catch语句中执行System.exit(0)时,会退出java虚拟机,所以不会执行finally语句块。

 

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

 

 

实验任务七:动手动脑4

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

2.代码:

package demo;

 

import java.util.*;

class Abnormal extends Exception{

      

}

class Abnormal1 extends Exception{

      

}

 

public class TestScore {

       static TestScore A=new TestScore();

       public static void Score(){

               Scanner in=new Scanner(System.in);

           System.out.print("请输入成绩(整数):");

           int v,u=1;

               String w=in.next();

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

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

                      {

                            

                             try

                             {

                                    throw new Abnormal();

                             }

                             catch(Abnormal e){

                                    u=0;

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

                                    break;

                             }

                      }

               }

               

               if(u==1){

                      v=Integer.parseInt(w);

                      if(v>100){

                             try{

                                    throw new Abnormal1();

                             }catch(Abnormal1 e){

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

                             }

                      }

                      else{

                          if(v<60){

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

                          }

                          else if(v>=60)

                          {

                                 if(v>70&&v<80){

                                        System.out.println("中!");

                                 }

                                 else if(v>=80&&v<90){

                                        System.out.println("良!");

                                 }

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

                                        System.out.println("优!");

                                 }

                                 else{

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

                                 }

                          }

                      }

               }

               else

               A.Score();

 

       }

      

     public static void main(String args[]){

           while(true){

            A.Score();

           }

     }

}

4.结果截图:

 

 

 

原文地址:https://www.cnblogs.com/ylx111/p/6099967.html