2021-4-27课堂代码


package edu.xcdq;

public class Demo01 {
    public static void main(String[] args) {
        int divisor = 100;
        int dividend = 30;
        try {
            System.out.println(divisor / dividend);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("捕捉到一个异常");
        }finally {
            System.out.println("不论如何都会执行这里的代码");
        }
        System.out.println("哈哈哈哈哈哈哈哈哈");
    }
}

package edu.xcdq;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        int []a=new int[2];
        Scanner scanner=new Scanner(System.in);
        int i=scanner.nextInt();
        int j=scanner.nextInt();
        try{
            a[0]=i;
            a[2]=j;
            System.out.println(a[0]/a[2]);
        }catch (IndexOutOfBoundsException e){
            System.out.println("数组越界异常");
        }catch (NumberFormatException e){
            System.out.println("数据格式不正确异常");
        }catch (ArithmeticException e){
            System.out.println("算数异常");
        }
    }
}
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        int []a=new int[2];
        Scanner scanner=new Scanner(System.in);
        int i=scanner.nextInt();
        int j=scanner.nextInt();
        try{
            a[0]=i;
            a[2]=j;
            System.out.println(a[0]/a[2]);
            //Array  Index  Outof  Bounds  Exception  数组  索引  超出  边界  异常
            //Input  Mismatch  Exception  输入  不匹配  异常
            //Arithmetic  Exception  数学数字  异常
        }catch (IndexOutOfBoundsException| InputMismatchException|ArithmeticException e){
            System.out.println("数组越界异常");
            System.out.println("数据格式不正确异常");
            System.out.println("算数异常");
            System.out.println("以上异常中的一个");
        }
    }
}

package edu.xcdq;

public class Demo04 {
    public static void main(String[] args)throws Exception {
        /*try{
            setSex("变性人");
        }catch (Exception e){   //调用者处理异常
            e.printStackTrace();
            System.out.println("非男非女");
        }  */
            setSex("afsfas");

    }

    public static void setSex(String sex) throws Exception {  //声明异常
        if (!(sex.equals("男")||sex.equals("女"))  ){
            throw new Exception("处理不了的异常,扔出去");   //抛出异常
        }
    }
}

package edu.xcdq;

public class Demo05 {
    public static void main(String[] args){
        try{
            setSex("双性人");
        }catch (Exception e){
            System.out.println("调用者说处理过了");
        }
    }

    public static void setSex(System sex)throws SexException{
        if (!(sex.equals("男")||sex.equals("女"))){
            throw new SexException("发现一个不对劲的");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

言&生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值