Java 抛出 RuntimeException 的好处是什么 [英] Java what is the benefit of Throwing RuntimeException

查看:73
本文介绍了Java 抛出 RuntimeException 的好处是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

沿着方法签名 b/c 声明 (Unchecked Exception)throws 异常有什么好处,它不会强制调用者保持在 try catch 块中.

What is the benefit of declaring (Unchecked Exception)throws exception along method signature b/c it does not force caller to keep in try catch block.

public void testRuntimeEx()throws RuntimeException{

if(1==1){throw new RuntimeException()}

}

//Caller method
public void testCaller(){
// not necessery to handle even caller does not known which RuntimeException might be throws then what is the benefit throws clause with method signature
testRuntimeEx(); 

}

推荐答案

如果你抛出自定义的未检查异常,它会变得更有意义,而不是捕获系统异常,并且未检查异常也不会强制捕获.

if you throw custom unchecked exception it becomes more meaningfull rather catching system exception and unchecked exception does not force to catch too.

这篇关于Java 抛出 RuntimeException 的好处是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆