呦呵呵丶

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

动手动脑

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

源代码

package homework7;

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

运行截图

分析原因 

将可能要出现错误的语句放到try语句中,当程序检测到这个错误时,用catch语句捕获并且处理该错误,如果发生了异常,程序会由try语句跳到catch语句。但如果没有提供合适的异常处理代码,JVM会结束整个应用程序。

2、实验内容:请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。

源代码

package homework7;


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

}

  运行截图

分析:当try....catch....finally语句嵌套使用时,程序会先执行内层的try语句,当检测到错误时由catch语句捕获,而内层捕获并处理了异常,外层就不会再捕获,只执行finally语句块,如果内层并未处理该异常,则外层捕获并处理异常,执行外层的finally语句,比如将上述代码修改后的结果并不相同

3、实验内容:阅读以下代码(CatchWho.java),写出程序运行结果

源代码

package homework7;

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

  截图

 

4、.实验内容:写出CatchWho2.java程序的运行结果

源代码

package homework7;

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

  运行截图

 

5、实验内容:辨析finally语句块一定会执行吗?请通过 SystemExitAndFinally.java示例程序回答上述问题

源代码

package homework7;


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


}

  运行截图

分析:如果上述代码修改为:

package homework7;

import javax.swing.*;

public class SystemExitAndFinally1{

       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语句块,一般情况下,finally语句块一定执行,但是当异常处理代码catch中执行System.exit(0)时,会退出java虚拟机,也就不再执行finally语句块。

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

  实验要求:要求有健壮性,无论用户输入什么都不会崩

源代码

package homework7;

import java.util.*;

class Unusual extends Exception{

      

}

class Unusual1 extends Exception{

      

}

public class Test {

       static Test A=new Test();

       public static void Dongnao(){

               Scanner in=new Scanner(System.in);

           System.out.print("请输入一个整数:");

           int t=1;

            int m1;

               String m=in.next();

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

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

                      {

                             try{

                                    throw new Unusual();

                             }catch(Unusual e){

                                    t=0;

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

                                    break;

                             }

                      }

               }

               if(t==1){

                      m1=Integer.parseInt(m);

                      if(m1>100){

                             try{

                                    throw new Unusual1();

                             }catch(Unusual1 e){

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

                             }

                      }

                      else{

                          if(m1<60){

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

                          }

                          else if(m1>=60)

                          {

                                 if(m1>65&&m1<75){

                                        System.out.println("中");

                                 }

                                 else if(m1>=75&&m1<90){

                                        System.out.println("良");

                                 }

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

                                        System.out.println("优");

                                 }

                                 else{

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

                                 }

                          }

                      }

               }

               else

                     A.Dongnao();

       }

     public static void main(String args[]){

           while(true){

            A.Dongnao();

           }

     }

}

  

 

运行截图

 

归纳总结

1、依据对本讲多个示例程序的分析,请自行归纳总结出Java多层嵌套异常处理的基本流程。

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

 

 

posted on 2017-11-16 22:21  呦呵呵丶  阅读(129)  评论(0编辑  收藏  举报