多态与异常处理动手动脑

1.运行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");
        
        }
    
    }

}

结果:

in Level 1
in Level 2
in Level 3
Level 3:class java.lang.ArithmeticException
In Level 3 finally
In Level 2 finally
In Level 1 finally

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

源代码:

import java.util.InputMismatchException;
import java.util.Scanner;
import javax.swing.JOptionPane;
/*编写一个程序,此程序在运行时要求用户输入一个    整数,代表某门课的考试成绩
 程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
要求程序必须具备足够的健壮性,不管用户输入什么样的内容,都不会崩溃。
*/
public class Test {
    public static void main(String[] args){
        int score;
        int a=1;
        while(a==1){
        System.out.println("请输入考试成绩:");
        try{
            Scanner scan=new Scanner(System.in);
            score=scan.nextInt();
            if(score<0||score>100)
                {
                    System.out.println("输入错误!!!,请重新输入");
                }
            else
                {
                    if(score>=90&&score<=100)
                        System.out.println("优。");
                    if(score>=80&&score<90)
                        System.out.println("良。");
                    if(score>=70&&score<80)
                        System.out.println("中。");
                    if(score>=60&&score<70)
                        System.out.println("及格。");
                    if(score>=0&&score<60)
                        System.out.println("不及格。");
                    a=0;
                    scan.close();
                }
            }
        catch(InputMismatchException e){
            System.out.println("输入错误!!!,请重新输入"+e.getMessage());
            }
        catch(Exception e)
            {
                System.out.println("输入错误!!!,请重新输入"+e.getMessage());
            }
        finally
            {
                JOptionPane.showConfirmDialog(null,"OK");
            }
        }
    }
    }

结果:

posted @ 2015-11-15 20:01  wzflbc  阅读(109)  评论(0编辑  收藏  举报