zoukankan      html  css  js  c++  java
  • throw throws try catch finally return

    throw
    throw 语句用于抛出异常,例如 throw new EOFException()。
     
    throws
    当使用throw 语句抛出checked 异常后,可以不用捕获异常并处理,而是使用throws 语句传递异常给本方法的调用者,调用者根据自身情况对异常进行捕获或者继续传递异常给它的调用者,如下:
    public void read () throws IOException(
    xxx
    throw new IOException();
    }
    当调用了包含throws checked 异常语句的方法后,可以用同样方式处理。
     
    try/catch
    如果打算捕获并处理异常,那么可以使用 try/catch 语句块,一般格式如下:
    try{
    xxx
    }
    catch (IOException e){
    xxx
    }
    catch (Exception e){
    xxx
    }
    在Java SE 7中,也可以合并多个catch语句,如下:
    try{
    xxx
    }
    catch (IOException e | Exception e){
    xxx
    }
    如果在 try 语句块中的任何代码抛出了一个在 catch 语句块中捕获的异常类,那么程序将跳过 try 语句块的其余代码并且执行对应 catch 语句块中的代码。如果方法中的任何代码拋出了一个在 catch 语句块中没有声明的异常类型,那么这个方法就会立刻退出。
    catch 语句块在处理异常时,可以对异常进行包装后重新抛出,如下:
    try{
    xxx
    }
    catch (SQLException e){
    throw new ServletException ("xxxx") ;
    }
     
    finally
    不管try 语句块是否抛出异常,甚至在try 语句块里有return 语句,finally 语句块中的代码最后都会被执行。它通常用来回收系统资源,如下:
    InputStream in = ..
    try{
    xxx
    }
    finally{
    in.close();
    }
    也可以带有catch 语句块,如下:
    InputStream in = ..
    try{
    xxx
    }
    catch (IOException e){
    xxx
    }
    finally{
    in.close();
    }
    注意,当执行了System.exit()、JVM崩溃、当前线程被其他线程中断或者杀死,那么finally 语句块不会执行。
     
    return
    return 是用来退出当前方法,并返回一个值给调用者。
     
    综合
    看一个例子:
    public class Demo {
    public static int test() {
    int x = 1;
    try {
    return ++x;
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    ++x;
    }
    return x;
    }
     
    public static void main(String[] args) throws Exception {
    int y = test();
    System.out.println(y);
    }
    }
     
    以上例子中,最终输出2。
     
    原因是什么?
    翻看官方的jvm规范就会把一切的谜团解开了:
    If the try clause executes a return, the compiled code does the following:
    Saves the return value (if any) in a local variable.
    Executes a jsr to the code for the finally clause.
    Upon return from the finally clause, returns the value saved in the local variable.
    简单翻译下:
    如果try语句里有return,那么代码的行为如下:
    1.如果有返回值,就把返回值保存到局部变量中
    2.执行jsr指令跳到finally语句里执行
    3.执行完finally语句后,返回之前保存在局部变量表里的值
    根据上面的说明就可以轻易地解释为什么是2了。
    当执行到return ++x;时,jvm在执行完++x后会在局部变量表里另外分配一个空间来保存当前x的值。
    注意,现在还没把值返回给y,而是继续执行finally语句里的语句。等执行完后再把之前保存的值(是2不是x)返回给y。所以就有了y是2不是3的情况。
    其实这里还有一点要注意的是,如果你在finally里也用了return语句,比如return +xx。那么y会是3。因为规范规定了,当try和finally里都有return时,会忽略try的return,而使用finally的return。
     
    查看Test.class的字节码我们同样也可以很轻松地知道为什么是2而不是3:
    大概讲讲指令操作顺序:
    iconst_1: 把常数1进栈 ---> istore_1: 栈顶元素出栈并把元素保存在本地变量表的第二个位置里(下标为1的位置里) ---> iinc 1, 1 : 本地变量表的第二个元素自增1 --->iload_1:第二个元素进栈 ---> istore_2:栈顶元素出栈并把元素保存在本地变量表的第2个位置里 ---> iinc 1, 1 : 本地变量表的第二个元素自增1 ---> iload_2:第二个元素进栈 (注意,此时栈顶元素为2)---> ireturn:返回栈顶元素。
    后面的指令是要在2-7行出现异常时在跳到12行的,这个例子没出现异常,不用关注。
    上面流程栈和本地变量表的情况如下图:
     
     
     
    参考
    https://www.cnblogs.com/averey/p/4379646.html
  • 相关阅读:
    【leetcode】1215.Stepping Numbers
    【leetcode】1214.Two Sum BSTs
    【leetcode】1213.Intersection of Three Sorted Arrays
    【leetcode】1210. Minimum Moves to Reach Target with Rotations
    【leetcode】1209. Remove All Adjacent Duplicates in String II
    【leetcode】1208. Get Equal Substrings Within Budget
    【leetcode】1207. Unique Number of Occurrences
    【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
    【leetcode】LCP 3. Programmable Robot
    【leetcode】LCP 1. Guess Numbers
  • 原文地址:https://www.cnblogs.com/gjb724332682/p/8747878.html
Copyright © 2011-2022 走看看