我用try-catch捕获了所有的异常,但是在这里没法处理,需要抛出到上级处理,但是匿名函数没法写throws,所以只能抛出RuntimeException,有什么办法吗?
1条回答
- Mi_RenYu 2023-09-06 11:35关注
> public class Main { public static void main(String[] args) { try { // 可能抛出Exception异常的代码 throwCheckedException(); } catch (Exception e) { // 将Exception异常转换为RuntimeException并抛出 throw new UncheckedException(e); } } public static void throwCheckedException() throws Exception { // 抛出一个Exception异常 throw new Exception("Checked Exception"); } public static class UncheckedException extends RuntimeException { public UncheckedException(Throwable cause) { super(cause); } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报