Java异常

RuntimeException

Java程序在运行的过程中产生异常,就会中断程序的正常执行,为了保证程序在出现异常时 依然能继续执行,就需要对异常进行处理。异常处理就是当程序运行发生不可预知的错误时, 程序能获得异常并进行处理。
Exception 类:指由程序和外部环境引起的错误,它是可以被捕获且可能恢复的异常情况, 它包括 RuntimeException 类异常与其他 Exception 类异常。
RuntimeException 类:即运行时异常,表示编程时所存在的隐患或错误在运行期间所产 生的异常,如数组越界异常、空指针异常、除 0 运算等。Java 编译器允许对这类异常不进行 处理,而在运行期间抛出一个异常信息,告知程序员纠正错误。

异常例子

以BusinessException为业务异常,举个例子:

    public class BusinessException extends RuntimeException {

        private String code;

        public BusinessException() {
            super();
        }

        public BusinessException(String message, String code) {
            super(message);
            this.code = code;
        }

        public BusinessException(String message) {
            super(message);
        }

        public BusinessException(String message, Throwable cause) {
            super(message, cause);
        }

        public BusinessException(String message, String code, Throwable cause) {
            super(message, cause);
            this.code = code;
        }

        public BusinessException(Throwable cause) {
            super(cause);
        }

        public String getErrorCode() {
            return this.code;
        }
        // 封装各种异常抛法, 继承了RuntimeException都可以被手动抛出
        public static void throwWithNull(){
            throw new BusinessException();
        }

        public static void throwWithMessage(String message){
            throw new BusinessException(message);
        }
        // BusinessError在下面会讲到
        public static void throwWithMsgAndCode(BusinessError error){
            throw new BusinessException(error.message, error.code);
        }

        public static void throwWithMsgAndCode(String message, String code){
            throw new BusinessException(message, code);
        }

        public static void throwBWithMsgAndCause(String message, Throwable cause){
            throw new BusinessException(message, cause);
        }
    }

枚举类错误

枚举类型是Java 5中新增特性的一部分,它是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类类型多了些特殊的约束,但是这些约束的存在也造就了枚举类型的简洁性、安全性以及便捷性。
在定义枚举类型时我们使用的关键字是enum,与class关键字类似,只不过前者是定义枚举类型,后者是定义类类型
枚举类型可以像类(class)类型一样,定义为一个单独的文件,当然也可以定义在其他类内部

    public enum BusinessError {
        // 括号第一位对应code,第二位对应message
        SUCCESS("0", "success"),
        SYSTEM_ERROR("1001", "system error"),
        ILLEGAL_ARGUMENT("1002", "illegal parameter"),
        CONVERT_ERROR("1010", "properties convert error"),
        public String code;
        public String message;

        BusinessError(String code, String message){
            this.code = code;
            this.message = message;
        }
    }

枚举错误结合异常

在代码中直接调用

    BusinessException.throwWithMsgAndCode(BusinessError.ILLEGAL_ARGUMENT);

throw和throws

throw一般会用于程序出现某种逻辑时程序员主动抛出某种特定类型的异常

    public static void main(String[] args) {
        String s = "abc";
        if(s.equals("abc")) {
            throw new NumberFormatException();
        } else {
            System.out.println(s);
        }
    }

throws是方法可能抛出异常的声明。(用在声明方法时,表示该方法可能要抛出异常)

public static void function() throws NumberFormatException{
        String s = "abc";
        System.out.println(Double.parseDouble(s));
    }

    public static void main(String[] args) {
        try {
            function();
        } catch (NumberFormatException e) {
            System.err.println("非数据类型不能转换。");
            //e.printStackTrace();
        }
}

在捕捉异常的try{…}语句块中,如果出现了异常,则该语句(出现异常的语句)后的程序语句都不执行,而是跳到catch{…}语句块中执行异常的处理

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值