try { //调用带throws声明的办法,必须显式捕获该异常 //不然,必须在main办法中再次声明抛出 throwChecked(-3); } catch (Exception e) { System.out.println(e.getMessage()); } //调用抛出Runtime异常的办法既可以显式捕获该异常, //也可不睬会该异常 throwRuntime(3); } public static void throwChecked(int a)throws Exception { if (a > 0) { //自行抛出Exception异常 //该代码必须处于try块里,或处于带throws声明的办法中 throw new Exception("a的值大年夜于0,不相符请求"); } } public static void throwRuntime(int a) { if (a > 0) { //自行抛出RuntimeException异常,既可以显式捕获该异常 //也可完全不睬会该异常,把该异常交给该办法调用者处理 throw new RuntimeException("a的值大年夜于0,不相符请求"); } } } 弥补:throwChecked函数的别的一种写法如下所示:
- public static void throwChecked(int a)
- {
- if (a > 0)
- {
- //自行抛出Exception异常
- //该代码必须处于try块里,或处于带throws声明的办法中
- try
- {
推荐阅读
开源要「开」得安全高效:开源云计算的五大发展趋势
编者按:以前几年,开辟者几乎都邑用到开源。开源现象日益广泛,有赖于业内人士的聪明和尽力。不过,更重要的┞氛样开源本身的优势:可以或许随便马虎整合多种多样的开源解决筹划。在业内>>>详细阅读
本文标题:Java异常的深入研究与分析
地址:http://www.17bianji.com/lsqh/35587.html
1/2 1