
一、try-catch-finnally
1)try-catch-finally
ctrl + alt + t
程序员在代码中捕获发生的异常,自行处理。
| 1 | try{ | 
2)throws
将发生的异常抛出,交给调用者(方法)来处理,最顶级的处理者就是 JVM。
2)注意点

- 如果异常发生了那,异常后面的代码不会执行,直接进入catch快。 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20- public class exception2 { 
 public static void main(String[] args) {
 try{
 String str="13a";
 int i =Integer.parseInt(str);
 System.out.println("前面发生后面这个不会执行了");
 }catch (Exception e){
 System.out.println("出现异常进入catch"+e.getMessage());
 }
 System.out.println("代码继续执行trycatch后面的代码");
 }
 }finally {
 System.out.println("finaly被执行了");
 }
 =====
 出现异常进入catchFor input string: "13a"
 finaly被执行了
 代码继续执行trycatch后面的代码- 可以由多个 catch 语句,捕获不同的异常。 但要求==父类异常在后,子类异常在前==。比如(EXCEPTION 在后,nullpointexception 在前。) 。如果报错,==只会匹配一个 catch==
   
- 不管结果怎么样,最终都要执行 finally 的方法  
 案例 
 这里出现异常没有进行 catch ,则直接到 finally ,然后就退出该方法了,不会继续执行后面的代码。
二、throw
如果程序抛出了异常,而没有进行异常捕获处理,那默认就是 throw 抛出这个异常。

三、案例分析
1.try/catch finally
1.1

finally 优先级很高,必须执行!!!!。
1.2
return i++; ,i++会执行,但因为必须执行 finally ,所以先不能执行 return。
四、总结

 
         
              