zoukankan      html  css  js  c++  java
  • Java 09

    一。多层的异常捕获

    预测结果:ArrayIndexOutOfBoundsException/内层try-catch
    发生ArithmeticException

    预测结果:ArrayIndexOutOfBoundsException/外层try-catch

    二:异常抛出几点注意

         1.异常时立刻跳出,寻找 catch语句处理异常,一旦异常被处理好之后,那么,其余的catch语句就不会再被执行,但是,finally语句是无论有没有异常都会执行的语句,当然这也不是绝对的。

         2.得知finally不执行的几种情况

    1)finally语句块中发生了异常。
    2)在前面的代码中用了System.exit()退出程序。
    3)程序所在的线程死亡。
    4)关闭CPU

    三。课堂成绩代码

    import java.util.Scanner;
    class ScException extends Exception{
        public ScException(){
            
        }
    }
    public class A {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            boolean f=true,h=true,q=true;
            Scanner s=new Scanner(System.in);
            int Sc=0;String ss;
            while(f){
                System.out.println("Please input a score:");
                ss=s.nextLine();
                for(int i=0;i<ss.length();i++){
                    if((ss.charAt(i)<'0')||(ss.charAt(i)>'9'))
                        break;
                    else{
                         q=false;
                         h=false;
                    }
                }
                if(q){
                    try{
                        throw new ScException();
                    }
                    catch(ScException e){
                        System.out.println("格式错误");
                    }
                } 
                if(!h) //输入的是数字
                {
                    Sc=Integer.parseInt(ss);
                    if((Sc<0)||(Sc>100))
                    {
                        try{
                            ScException x= new ScException();
                            throw x;
                        }
                        catch(ScException x){
                            System.out.println("输入异常,请输入小于100正整数");        
                        }
                    }
                    else
                        f=false;
                }
           
            }
            if(Sc<60)
                System.out.println("不及格");
            else if(Sc<70)
                System.out.println("及格");
            else if(Sc<80)
                System.out.println("中");
            else if(Sc<90)
                System.out.println("良");
            else if((Sc<100)||(Sc==100))
                System.out.println("优");
                
        }
    
    }

  • 相关阅读:
    Leetcode 12. Integer to Roman
    Leetcode 133. Clone Graph
    Leetcode 199. Binary Tree Right Side View
    Leetcode 200. Number of Islands
    React通过Ajax获取数据
    canvas鼠标点击划线
    制作图片墙
    CSS制作翻牌特效
    输入框制作方法
    初来咋到
  • 原文地址:https://www.cnblogs.com/limu/p/6102878.html
Copyright © 2011-2022 走看看