【Java】自定义异常

本文介绍了一个自定义异常类LittleCatException的实现过程,该类继承自RuntimeException,并增加了错误码的功能。通过不同构造方法的支持,可以灵活地传递错误信息和原始异常。此外,还提供了一段测试代码来演示如何使用这个自定义异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

package com.littlecat.cbb.exception;

public class LittleCatException extends RuntimeException
{
    private static final long serialVersionUID = 1L;
    private String errorCode;

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

    public LittleCatException(String errorCode, String message)
    {
        super(message);
        this.errorCode = errorCode;
    }

    public LittleCatException(String errorCode, String message, Throwable cause)
    {
        super(message, cause);
        this.errorCode = errorCode;
    }

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

    public String getErrorCode()
    {
        return errorCode;
    }

    public void setErrorCode(String errorCode)
    {
        this.errorCode = errorCode;
    }

    //测试代码
    public static void main(String[] args)
    {
        try
        {
            System.out.println(100 / 0);
        }
        catch (Exception e)
        {
            System.out.println(new LittleCatException("haha", e).getMessage());
            throw new LittleCatException("haha", e);
        }
        throw new LittleCatException("haha");
    }
}

 

  

 

转载于:https://www.cnblogs.com/njlittlecat/p/9580693.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值