zoukankan      html  css  js  c++  java
  • Java(异常处理)动手动脑

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

    import javax.swing.*;
    
    class AboutException {
       public static void main(String[] a) 
       {
          float i=1, j=0, k;
          k=i/j;
          System.out.println(k); 
        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

    { //用于“善后” 的代码 }

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

    2>阅读以下代码(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"); 
            } 
        } 
    }

    运行结果:

    ArrayIndexOutOfBoundsException/内层try-catch
    发生ArithmeticException

    结果分析:当内层捕获异常并处理后,外层则不再捕获该异常。

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

    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示例,再运行它,观察其输出并进行总结。

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

    运行结果:

    结果分析:当外层异常未被处理时,内层异常不会被处理并且finally也不会执行,当有多层嵌套的finally语句时,异常在不同层次不同位置抛出时,也会导致不同的finally语句块执行顺序。

    5>finally语句块一定会执行吗?

    请通过 SystemExitAndFinally.java示例程序回答上述问题

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

    运行结果:

    System.exit(0);

    //System.exit(0);    

    运行结果:首先只有与finally对应的try语句得到执行的情况下finally语句才会执行,但如果finally语句之前出现例如System.exit(0) 等使Java虚拟机停止运行的语句时finally语句也不会被执行。

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

    import java.util.Scanner;
    class MyException extends Exception
    {
        MyException(String str)
        {
            super(str);
        }
    }
    class GradeTest{
        private double grade;
        public void InputGrade(String s)
        {
            boolean flag = true;
            try
            {
                for(int i = 0;i<s.length();i++)
                {
                    if((s.charAt(i) < '0' || s.charAt(i) > '9')&& s.charAt(i) != '.')
                        flag = false;
                }
                if(!flag)
                {
                    throw new MyException("请输入数字!");
                }
                try{
                    grade = Double.parseDouble(s);
                    if(grade < 0.0 || grade > 100.0)
                    {
                        throw new MyException("请输入0.0到100.0的数据!");
                    }
                    if(grade < 60.0)
                        System.out.println("不及格");
                    else if(grade <70.0)
                        System.out.println("及格");
                    else if(grade < 80.0)
                        System.out.println("中");
                    else if(grade < 90.0)
                        System.out.println("良");
                    else
                        System.out.println("优");
                }
                catch(MyException e)
                {
                    System.out.println(e.getMessage());
                }
            }
            catch(MyException e)
            {
                System.out.println(e.getMessage());
            }
        }
    }
    public class Grade {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner in = new Scanner(System.in);
            GradeTest G = new GradeTest();
            System.out.println("请输入成绩:");
            String str = in.nextLine();
            G.InputGrade(str);
            in.close();
        }
    
    }

    运行结果:

  • 相关阅读:
    西卡编程教学 C语言教学视频(共32课更新完毕) 『 西卡教学 』 西卡学院 Powered by Pureing Labs!
    大图片新闻的体验还是不错的
    分享:用php抓取网页内容方法总结
    “深圳文献港”昨日正式开通_综合新闻_财经_腾讯网
    分享:EJDB 1.0.37 发布,嵌入式 JSON 数据库引擎
    分享:MetaModel 3.2.5 发布,数据库元模型
    RQ: Simple job queues for Python
    分享:开源主机项目 Ouya 发布 SDK
    RQ 简单的任务队列 品牌控
    信息论、推理与学习算法(翻译版)
  • 原文地址:https://www.cnblogs.com/ttzz/p/6099414.html
Copyright © 2011-2022 走看看