zoukankan      html  css  js  c++  java
  • 多个catch块

    多个catch块 

    catch块只能处理一类异常,当try块中的语句组可能抛出多种异常时,就需要有多个catch块来分别处理各种异常。
    例如:使用初始化参数输入两个数字,并进行除法操作

    public class seven3{
        public static void main(String[] args){
            System.out.println("------开始--------");
            int i=0;
            int j=0;
            try{
                String str1=args[0];   //args用来存放变量数组
                String str2=args[1];
                i=Integer.parseInt(str1);    //Integer.parseInt(String) 将字符数据类型转换为Integer整数型
                j=Integer.parseInt(str2);        //遇到一些不能被转换为整型的字符时,会抛出异常
                int temp=i/j;            
                System.out.println("两个数字相除结果:"+temp);
                System.out.println("---------结束-------");
            }
            catch(ArithmeticException e){        //捕获算术异常
                System.out.println("算术异常:"+e);
            }
            catch(NumberFormatException e){        //捕获数字转换异常
                System.out.println("数字转换异常:"+e);
            }
            catch(ArrayIndexOutOfBoundsException e){    //捕获数字越界异常
                System.out.println("数字越界异常:"+e);
            }
            System.out.println("*********计算结束********");
        }
    }

  • 相关阅读:
    BZOJ3992 [SDOI2015]序列统计
    BZOJ3991 [SDOI2015]寻宝游戏
    BZOJ4007 [JLOI2015]战争调度
    BZOJ4006 [JLOI2015]管道连接
    BZOJ4004 [JLOI2015]装备购买
    P2567 [SCOI2010]幸运数字
    P1447 [NOI2010]能量采集
    比赛-Round 2 (11 Jul)
    题解-弹飞绵羊 (HNOI2015)
    归并排序模板
  • 原文地址:https://www.cnblogs.com/l666/p/9656935.html
Copyright © 2011-2022 走看看