自定义异常的核心在于根据异常是否需要强制处理来选择继承runtimeexception或exception。继承runtimeexception适用于程序逻辑错误,如参数校验失败,无需强制处理,编译器不检查;继承exception适用于外部因素导致的错误,如文件不存在,必须try-catch或throws声明。创建自定义异常需定义类并添加属性方法,如mycustomexception含errorcode,mycustomruntimeexception含detailmessage。抛出时dosomething需声明throws,而dosomethingelse无需声明。处理时分别捕获并输出信息。避免滥用自定义异常,优先使用标准异常类,仅在必要时扩展。多线程中可用uncaughtexceptionhandler或future.get()处理异常。spring boot中可结合@controlleradvice、@exceptionhandler、errorattributes及responseentityexceptionhandler实现全局异常管理。
定义自定义异常,说白了就是为了更精准地处理程序中可能出现的各种“意外”。至于继承 RuntimeException 还是 Exception,这可不是随便选的,背后藏着不少门道。简单来说,RuntimeException 是非受检异常,编译器不会强制你必须处理;而 Exception 是受检异常,不 try-catch 或者 throws 声明,编译器就跟你过不去。
解决方案
自定义异常,核心在于创建一个新的类,继承自 Exception 或者 RuntimeException,然后根据你的需求添加一些自定义的属性和方法。
创建自定义异常类
// 继承 Exception(受检异常) class MyCustomException extends Exception { private int errorCode; public MyCustomException(String message, int errorCode) { super(message); this.errorCode = errorCode; } public int getErrorCode() { return errorCode; } } // 继承 RuntimeException(非受检异常) class MyCustomRuntimeException extends RuntimeException { private String detailMessage; public MyCustomRuntimeException(String message, String detailMessage) { super(message); this.detailMessage = detailMessage; } public String getDetailMessage() { return detailMessage; } }
抛出自定义异常
public void doSomething(int value) throws MyCustomException { // 必须声明 throws if (value < 0) { throw new MyCustomException("Value cannot be negative", 1001); } // ... } public void doSomethingElse(String input) { // 无需声明 throws if (input == null || input.isEmpty()) { throw new MyCustomRuntimeException("Input cannot be null or empty", "Invalid input received"); } // ... }
处理自定义异常
try { doSomething(-1); } catch (MyCustomException e) { System.err.println("Caught MyCustomException: " + e.getMessage() + ", Error Code: " + e.getErrorCode()); } try { doSomethingElse(null); } catch (MyCustomRuntimeException e) { System.err.println("Caught MyCustomRuntimeException: " + e.getMessage() + ", Detail: " + e.getDetailMessage()); }
考虑一下这样的场景:一个方法被调用了无数次,每次调用都强制进行异常处理会显得非常繁琐,甚至影响代码的可读性。比如,空指针异常 NullPointerException,你总不能每次用到对象都 try-catch 一下吧?继承 RuntimeException 的异常通常表示程序逻辑错误,这种错误应该在开发阶段就避免,而不是在运行时依赖异常处理。
如果你的异常是由于外部因素导致的,比如网络连接中断、文件不存在等,这些情况是程序无法完全控制的,那么就应该继承 Exception。 强制要求调用者处理这些异常,可以提高程序的健壮性。
自定义异常虽然强大,但也不能滥用。过多的异常定义会让代码变得复杂,难以维护。
在多线程环境中,异常的处理会更加复杂。你需要确保每个线程都能正确地处理异常,避免程序崩溃。
Spring Boot 提供了强大的异常处理机制,可以让你轻松地处理自定义异常。
总之,选择继承 RuntimeException 还是 Exception,取决于你的应用场景和设计目标。理解它们的区别,合理地使用自定义异常,可以提高代码的质量和可维护性。
以上就是如何正确定义自定义异常?继承RuntimeException和Exception的关键选择依据是什么?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号