对于try catch放在能够很好地处理例外的位置

Exception有一个message属性。在使用catch的时候可以调用:

Catch(IOException e){System.out.println(e.message())};

Catch(IOException e){e.printStackTrace()};

上面这条语句回告诉我们出错类型所历经的过程,在调试的中非常有用。

 

开发中的两个道理:

①如何控制try的范围:根据操作的连动性和相关性,如果前面的程序代码块抛出的错误影响了后面程序代码的运行,那么这个我们就说这两个程序代码存在关联,应该放在同一个try中。

① 对已经查出来的例外,有throw(积极)和try catch(消极)两种处理方法。

对于try catch放在能够很好地处理例外的位置(即放在具备对例外进行处理的能力的位置)。如果没有处理能力就继续上抛。

 1 package TomText;
 2 //使用递归方法计算3、4和5的阶乘。
 3 public class TomText_50 {
 4     public static void main(String[] args) {
 5         System.out.println("Factorial of 3 is " + fact(3));
 6         System.out.println("Factorial of 4 is " + fact(4));
 7         System.out.println("Factorial of 5 is " + fact(5));
 8     }
 9     //递归方法,求出参数n的阶乘并返回阶乘值
10     static int fact(int n) {
11         int result; 
12         if(n==1)
13             return 1;
14         result = fact(n-1) * n;
15         return result; 
16     }
17 
18 }
原文地址:https://www.cnblogs.com/borter/p/9419406.html