码迷,mamicode.com
首页 > 其他好文 > 详细

09-异常处理动手动脑及课后实践性问题总结

时间:2016-11-24 09:05:34      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:检测   ima   for   cep   结束   用户输入   相同   执行   java   

一.动手动脑:请阅读并运行AboutException.Java示例,然后通过后面的几页ppt了解Java中实现异常处理的基础知识。

(1)源代码:

import javax.swing.*;

class AboutException {
   public static void main(String[] a) 
   {
      double 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"+k);//正无穷
     }
        
  }
}

(2)结果分析:

Java中的异常捕获语句

Try{
   //可能发生运行错误的代码;
  }
  catch(异常类型     异常对象引用){
   //用于处理异常的代码
  }
  finally{
   //用于“善后” 的代码
  }

Java中所有可捕获的异常都派生自Exception类。

二.使用Java异常处理机制

?把可能会发生错误的代码放进try语句块中。
?当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。catch语句块中的代码用于处理错误。
?当异常发生时,程序控制流程由try语句块跳转到catch语句块。
?不管是否有异常发生,finally语句块中的语句始终保证被执行。
?如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

异常的“多态”特性

可以有多个catch语句块,每个代码块插获一种异常。在某个try块后有两个不同的catch块插获两个相同类型的异常是语法错误。

使用catch语句,只能捕获Exception类及其子类的对象。因此,一个捕获Exception对象的catch语句块可以捕获所有“可捕获”的异常。

将catch(Exception e)放在别的catch块前面会使这些catch块都不执行,因此Java不会编译这个程序。

三.动手动脑:多层的异常捕获-1

阅读以下代码(CatchWho.Java),写出程序运行结果:

(1)源代码:

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)

技术分享

(3)抛出两次异常,先处理内层的,再处理外层的。

四.动手动脑:多层的异常捕获-2

阅读以下代码(CatchWho2.Java),写出程序运行结果:

(1)源代码:

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"); 
        }//当不能比对成功,接着向下运行,比对成功,直接输出 
    } 
}

(2)

技术分享

(3)

当不能比对成功,接着向下运行,比对成功,直接输出 。

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

(1)源代码:

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

}

(2)

技术分享

(3)当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

六. finally语句块一定会执行吗?(SystemExitAndFinally.Java)

(1)

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


}

(2)

技术分享

(3)不一定执行。如在例子中,System.exit(0);导致finally并没有执行。

七.动手动脑:

编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。

(1)

import java.util.*;

class GeshiException extends Exception
{
    public GeshiException(String a)
    {
        super(a);
    }
}

public class Grade {
    public static void judge(int g){
        if(g>=0&&g<60)
            System.out.println("成绩不合格!");
        else if(g<70)
            System.out.println("成绩合格!");
        else if(g<80)
            System.out.println("成绩中!");
        else if(g<90)
            System.out.println("成绩良!");
        else if(g<=100)
            System.out.println("成绩优!");
        else
            System.out.println("但输入成绩不符");
    }
    public static void main(String[] args) {
        
        Scanner b=new Scanner(System.in);
        String b1;
        int c=1;
        System.out.println("请输入学生的成绩:");
        while(c==1)
        {
            boolean d=true;
            try
            {
                b1=b.nextLine();
                for(int i=0;i<b1.length();i++)
                {
                    if(b1.charAt(i)>57||b1.charAt(i)<46)
                    {
                        d=false;
                    }
                }
                if(!d)
                {
                    GeshiException a=new GeshiException("输入有误!不是的不是数字!");
                    throw a;
                }
                else
                {
                    c=2;
                    int g1=Integer.parseInt(b1);
                    System.out.print("输入正确!");
                    judge(g1);
                }
            }
         catch(GeshiException a)
         {
             System.out.print(a+"请重新输入:");
         }
       }
        b.close();
    }
}

(2)

技术分享

 

09-异常处理动手动脑及课后实践性问题总结

标签:检测   ima   for   cep   结束   用户输入   相同   执行   java   

原文地址:http://www.cnblogs.com/zhangjiabei/p/6096218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!