zoukankan      html  css  js  c++  java
  • 异常处理动手动脑

    1.多层的异常捕获

    结果截图

     

    解释

    1)try抛出一个异常之后,程序会跳出try,不再执行try后边的语句,开始对catch进行匹配,处理异常;

    2)try嵌套中,抛出的异常只有被处理才可以按顺序抛出下一个异常,如果不处理,程序就终止;

    3)try抛出异常之后,就跳出了try语句,内层catch无法捕获就继续向外抛,所以外层也就有异常,外层语句不执行,第二个程序 throw  new ArithmeticExcepption没有执行。

    4)第三个程序,try第一层第二层没有异常不用捕获,执行完之后到第三层,除0有异常,catch捕获,执行第三层的finally然后,顺序执行第二层,第一层的finally。

    5)try语句嵌套从外层到内层执行,在try语句中,哪一层出错,哪一层就抛出异常,后边的try语句就不再执行,如果该层存在catch就进行相应的捕获,有该层的finally也执行,除非finally遇到不执行的情况;

    6)如果该层没有catch进行捕获,就向外抛出,去找catch,如果没有catch进行捕获,就终止程序。

    2.课后作业

    import java.util.Scanner;

    public class Exam {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    boolean f=true,h=true,q=true;
    int Sc=0;
    while(f){
    System.out.println("Please input a score:");
    Scanner s=new Scanner(System.in);
    String ss=s.nextLine();

    int a=ss.length();
    for(int i=0;i<a;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("输入异常,请输入整数");


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

    }

    }
    class ScException extends Exception{
    public ScException(){

    }
    }

  • 相关阅读:
    Service Name Port Number Transport Protocol tcp udp 端口号16bit
    linux linux 互传文件 win 不通过 ftp sftp 往linux 传文件(文件夹)
    soft deletion Google SRE 保障数据完整性的手段
    Taylor series
    Taylor's theorem
    Moving average
    REQUEST
    Unix file types
    mysqld.sock
    Tunneling protocol
  • 原文地址:https://www.cnblogs.com/sunshine-z/p/7846998.html
Copyright © 2011-2022 走看看