使用Jeapordy的Alex Trebek在Java中尝试/捕获/最终返回语句


Hank Igoe: What are the Finger Lakes.
Alex Trebek: That is correct, Hank. You now have the board.
Hank: Let's try "Exceptional Java Programming" for $1000.
Alex: If a return statement is given in all 3 sections of a try/catch/finally block of a function, it's the block whose value will always determine the output of the function.
Hank: What is the finally block?
Alex: That is correct, and that concludes Double Jeapordy. When we come back the final Jeapordy category will be, "Presidential Tweets."

public  class MyTryReturn {

  public static String getVal() {
    int status = 0;
    try {
      System.out.println(++status + " in try");
      Integer inf =  1/0;

      System.out.println(++status  + " in try after dividing 1 by 0");
      return "oneOverZero";

    } catch(Exception e) {
      System.out.println(++status  + " in catch");
      return "two";

    } finally {
      System.out.println(++status  + " in finally");
      return "three";
    }
  }

  public static void main(String[] args) {

    String val = getVal();
    System.out.println(val);
  }
}

输出: 尝试1次 赶上2 最后三 三

评论