zoukankan      html  css  js  c++  java
  • Java异常处理

    .阅读代码(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("你输入的数字不符合要求");    
    
        }
    }
     
  • 相关阅读:
    606. Construct String from Binary Tree
    696. Count Binary Substrings
    POJ 3255 Roadblocks (次短路)
    POJ 2823 Sliding Window (单调队列)
    POJ 1704 Georgia and Bob (博弈)
    UVa 1663 Purifying Machine (二分匹配)
    UVa 10801 Lift Hopping (Dijkstra)
    POJ 3281 Dining (网络流之最大流)
    UVa 11100 The Trip, 2007 (题意+贪心)
    UVaLive 4254 Processor (二分+优先队列)
  • 原文地址:https://www.cnblogs.com/zhang12345/p/9942635.html
Copyright © 2011-2022 走看看