Java--异常机制

Java–异常机制

package com.zy.base;

import java.util.logging.Logger;

/**
 *description: Java--异常机制
 *@program: 基础语法
 *@author: zy
 *@create: 2023-02-23 21:34
 */
public class JavaException {
    public static void main(String[] args) {
        int a=1;
        int b=0;

        /*
            假设要捕获多个异常,异常类型从小到大。
            idea 快捷键:ctrl+alt+t:包裹选中代码选择关键字代码块
         */

        try {
            new JavaException().test(a,b);
            System.out.println("======================");
            if(b == 0){
                // 主动抛出异常,一般在方法中使用,抛给调用方
                throw new ArithmeticException();
            }
            System.out.println("======================");
            System.out.println(a/b);
        } catch (Exception e) {
            e.printStackTrace(); // 打印错误的栈信息
        } finally {
        }

        System.out.println("======================");
        try { // try监控区域
            new JavaException().a();
        }catch (Exception e){ // catch 想要捕获的异常类型:捕获异常
            System.out.println("Exception");
        }catch (Error error){
            System.out.println("Error");
        }catch (Throwable throwable){
            System.out.println("Throwable");
        }finally { // 处理善后工作,如关闭IO等
            System.out.println("finally");
        }

        System.out.println("======================");
        try {
            new JavaException().userDefinedFunction(11);
        } catch (UserDefinedException e) {
            Logger logger = Logger.getLogger("JavaException");
            logger.warning("UserDefinedException==>"+e);
            //System.out.println("UserDefinedException==>"+e);
        }
    }

    public void a(){
        b();
    }
    public void b(){
        a();
    }

    /**
     * @Description 方法上抛出异常,抛给调用方处理,便于程序后续运行
     * @author zy
     * [a, b]
     * void
     * @date 2023-2-23 21:50
     */
    public void test(int a,int b) throws ArithmeticException{
        System.out.println(a/b);
    }
    
    /**
     * @Description 自定义异常测试方法
     * @author zy
     * [number]
     * void
     * @date 2023-2-23 22:05
     */
    public void userDefinedFunction(int number) throws UserDefinedException {
        System.out.println("自定义异常测试传递的数字为:"+number);

        if(number>10){
            throw new UserDefinedException(number);
        }

        System.out.println("自定义异常测试方法正常运行");
    }
}

/**
 * @Description 自定义异常
 * @author zy
 * @date 2023-2-23 21:56
 */
class UserDefinedException extends Exception{

    // 传递数字>10即抛出异常

    private int number;

    public UserDefinedException(int number) {
        this.number = number;
    }

    /**
     * @Description 异常的打印信息
     * @author zy
     * []
     * java.lang.String
     * @date 2023-2-23 22:03
     */
    @Override
    public String toString() {
        return "UserDefinedException{" +
                "number=" + number +
                '}';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值