10.14 Replace Error Code with Exception 用异常取代错误码

xiaoxiao2025-10-03  10

使用异常将错误情况捕获或者抛出

更多精彩

更多技术博客,请移步 asing1elife’s blog

动机

某个方法返回一个特定的代码,用于表示某种错误情况使用异常定义错误才是最专业最通用的方法

案例

int withdraw(int amount) { if (amount > balance) { return -1; } else { balance = amount; return 0; } } void withdraw(int amount) throws BalanceException { if (amount > balance) { throw new BalanceException(); } balance > amount; }
转载请注明原文地址: https://www.6miu.com/read-5037300.html

最新回复(0)