2021-04-27

##Main

package edu.xcdq;

public class Main {

    public static void main(String[] args) {
	    int divisor = 10 ;
	    int dividend = 0 ;
	    try{
            System.out.println(divisor / dividend);//ArithmeticException    算数异常
        }catch (Exception e){
	        e.printStackTrace();
            System.out.println("捕获到一个异常");
        }finally {
            System.out.println("不管怎都会执行这个代码");
        }


        System.out.println("哈哈");
    }
}

##Demo01

package edu.xcdq;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @qvthor liuwenzheng
 * @date 2021/4/27 15:07
 */
public class Demo01 {
    public static void main(String[] args) {
        int [] a= new int[2] ;
        Scanner scanner = new Scanner(System.in);
        try {
            int i = scanner.nextInt();
            int j = scanner.nextInt();
            a[0] = i ;
            a[2] = j ;
            System.out.println(a[0]/a[2]);
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("数组越界异常");
        }catch (InputMismatchException e){
            System.out.println("数据格式不正确异常");
        }catch (ArithmeticException e){
            System.out.println("算数异常");
        }
    }
}

##Demo02

package edu.xcdq;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @qvthor liuwenzheng
 * @date 2021/4/27 15:15
 */
public class Demo02 {
    public static void main(String[] args) {
            int [] a= new int[2] ;
            Scanner scanner = new Scanner(System.in);
            try {
                int i = scanner.nextInt();
                int j = scanner.nextInt();
                a[0] = i ;
                a[2] = j ;
                System.out.println(a[0]/a[2]);
                // Array Index OutOf Bounds Exception 数组、索引 超出 边界 异常
                // Input Mismatch Exception  输入 不匹配 异常
                // Arithmetic Exception 数字数字 异常
            }catch (ArrayIndexOutOfBoundsException |InputMismatchException |ArithmeticException e){
                System.out.println("数组越界异常");

                System.out.println("数据格式不正确异常");

                System.out.println("算数异常");
            }
        }
    }



##Demo03

package edu.xcdq;

/**
 * @qvthor liuwenzheng
 * @date 2021/4/27 15:31
 */
public class Demo03 {
    public static void main(String[] args) throws Exception{       //继续向上声明,不处理
        /*try {
            steSex("双性人");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非男非女");
        }*/
        steSex("afwarf");
    }
    public static void steSex(String sex) throws Exception{  //声明异常
        if (!(sex.equals("男")|| sex.equals("女"))){
           throw new Exception("处理不了的异常,扔出去");  //抛出异常
        }
    }
}

##Demo04

package edu.xcdq;

/**
 * @qvthor liuwenzheng
 * @date 2021/4/27 15:46
 */
public class Demo04 {
    public static void main(String[] args){
        try {
            steSex("双性人");
        }catch (Exception e){
            System.out.println("调用者说处理过了");
        }
    }
    public static void steSex(String sex) throws SexException{  //声明异常
        if (!(sex.equals("男")|| sex.equals("女"))){
            throw new SexException("发现一个不对劲的");  //抛出异常
        }
    }
}

##SexException

package edu.xcdq;

/**
 * @qvthor liuwenzheng
 * @date 2021/4/27 15:44
 */
public class SexException extends Exception {
    public SexException(){
    }
    public SexException(String message){
        System.out.println(message);
        System.out.println("自定义的异常,知道非男飞女,但是没办法处理");
        System.out.println("..........");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值