• java异常处理课后作业


    1、动手动脑

    源码

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

      }
    }
     
     
    结果:
     
                  

     java把可能会发生错误的代码放进try语句块中,catch语句块中的代码用于处理错误,不管是否有异常发生,finally语句块中的语句始终保证被执行。如果没有提供合适的异常处理代码,JVM会结束掉整个应用程序。

    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 {
        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

    3、动手动脑

     源码


    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

    4、动手动脑

    源码


    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
    当存在try中有throw new Exception()时,finally不会执行
     
    课堂测验
     
    输入整数判断成绩等级,抛出异常
     
    import java.util.Scanner;
    class MyException extends Exception
    {
     public MyException(String messege)
     {
      super(messege);
     }
    }
    class numbertest //自定义异常类
    {
     public int score(int a) throws MyException  //当a<0或a>100时,抛出一个自定义异常
     {
      
      if(a<0 || a>100) 
      {
       throw new MyException("成绩输入有误");//抛出异常
      }  
      return a;//返回a
     }
    }
    public class test1{
     public static void main(String[] args) throws MyException
     {
      try
      {
       Scanner scan=new Scanner(System.in);
       System.out.println("请输入分数");
       int n = 0,i;
       n=scan.nextInt();
       numbertest k=new numbertest(); 
       try
       {
        int t=k.score(n);
        i=n/10;
        switch(i)
        {
        case 10:
        case 9:
         System.out.println("优");break;
        case 8:
         System.out.println("良");break;
        case 7:
         System.out.println("中");break;
        case 6:
         System.out.println("及格");break;
        default:
         System.out.println("不及格");break;
        }
       }
       catch(MyException e)
       {
        System.out.println(e);//输出
       }
       }
       catch(Exception e)//由于变量定义为int型,所以输入字符时,则输出该异常信息
       {
        System.out.println("输入格式不合法");//输出
       }
      }
    }
    结果

     

     
  • 相关阅读:
    Java常用类库(二):Iterator迭代器和子范围视图
    Java常用类库(一) : Object 和日期类的简单使用
    MyBatis的逆向工程
    AdminLTE介绍和zTree的简单使用
    MyBatis分页组件--PageHelper
    SpringAop--系统日志简例
    Shiro
    Mysql(三):多表查询和存储程序
    MySql(二):常见的那些个约束
    正则表达式一些用法
  • 原文地址:https://www.cnblogs.com/songxinai/p/11755410.html
Copyright © 2020-2023  润新知