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

动手动脑之异常处理

时间:2017-11-17 00:03:59      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:input   多层   导致   imp   总结   语言   多层嵌套   系统   scan   

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

技术分享

3.阅读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");
        
        }
    
    }

}

技术分享

原因:当有多个嵌套的try…catch…finally时,异常在不同位置被接受,可能会导致异常下面的finally语句块执行顺序。不管是否有异常发生,finally语句块中的语句始终保证被执行。

4.finally语句一定会被执行吗

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)可以终止程序,finally语句块一定会执行。

 5.自行归纳Java多层嵌套异常处理的基本流程

在java语言中,通常将可能出现异常的语句放入try{}语句中,将出现错误后需要执行的语句放入到catch{}语句中,将无论是否发生异常都要执行的语句放在finally{}语句中。当程序执行出现异常的时候,系统会抛出一个异常,然后由try{}语句中中出现异常的地方转到catch{}语句中。不过不管有没有异常产生,finally{}中的语句都将执行。如果系统出现系统错误或者运行Runtime异常,jvm会结束程序运行,不一定会执行finally{}中的语句。如果try{}中产生的异常在catch中没有处理,系统将停止程序,也不会执行finally中的语句。

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

import java.util.Scanner;
public class Test 
{
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.println("请输入分数:");

        String score=input.nextLine();
        while(true)
        {
        try
        {
            for(int i=0;i<score.length();i++)
            {
                
                if(!(score.charAt(i)>=48&&score.charAt(i)<=57))
                {
                    throw new MyException();
                }
            }
            int m = Integer.parseInt(score);//把字符串转换成整型
            String str="";
            if(m<60)
            {
                str="不及格";
            }
            else if(m<70)
            {
                str="及格";
            }
            else if(m<80)
            {
                str="中";
            }
            else if(m<90)
            {
                str="良";
            }
            else if(m<=100)
            {
                str="优";
            }
            else if (m>100||m<0)
            {
                throw new MyException();
            }
            System.out.println("分数等级为:"+str);    
            break;
        }
        catch(MyException A)
        { 
             A.error();
        }

        }
    }
}
class  MyException extends Exception{

    public void error()
    {
            System.out.println("你输入的数字不符合要求");    

    }
}

技术分享

 

动手动脑之异常处理

标签:input   多层   导致   imp   总结   语言   多层嵌套   系统   scan   

原文地址:http://www.cnblogs.com/lijing925/p/7846873.html

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