Abnormal and try, catch, finally shorthand

First, the unusual abbreviated

  1) Java exception and error base class Throwable (java.lang.Throwable), comprising Exception (abnormal) and Error (error).
  2) try and catch do not necessarily need to coexist, try in which the code is attempting to catch exceptions, catch this is an exception and can handle exceptions. If an exception is thrown in the try will catch block to execute, finally block is executed again; if not catch block, finally block may be performed directly, in a method of throwing an exception on the end, and finally the content is not executed, and the catch, and finally can not be omitted (each may be present alone).
  3) throws and the throw
    A) throws: After writing method in a statement, representation may throw an exception, the caller needs to handle the exception.
    b) throw: written in the method body, representation will throw an exception, or try ... catch processing, or throws throws.

 

SUPPLEMENTARY about try, catch, finally the

  On try-catch return inside the case: when the program execution method to try {} return statement, it will store the results to be returned to a temporary stack, and the program does not return immediately, but to perform finally { } in the program, as long as there is no return finally return value of the operation, he will not be returned to update the value of the temporary stack, it only covers the value of the variable. After execution, the main program will notify "finally the program is finished, the request may be returned to" this time, they will be taken out of the return value of the temporary stack.
  Conclusion: try before you perform any or catch the return statement, if there is, then finally, finally will first execute the statement. If you have finally return statement, then the program will return, so finally the return is sure to be return, the compiler of return in the finally implemented as a warning.

  the case of try-catch-finally analysis:
    1) Case 1: try {} catch () {} finally {} return;
      execution order -end
    2) Case 2: try {return;} catch () {} finally {} return;
      program code is executed before the try block return (arithmetic expression including a return statement) -> finally block is executed again, the execution of the last return try -> return statement after the finally block, because the program in the try return therefore no longer execute.
    3) Case 3: try {} catch () {return;} finally {} return;
      program executed first try, catch block if an exception is encountered performed -> abnormalities: the code before return is executed in a catch (including a return statement the expression evaluation) -> then execute all the code in the finally statement - "last execution return catch block -> finally no longer performed after the code -> no exception: after the try again then finally return.
    . 4 ) case 4: try {return;} catch () {} finally {return;}
      program code is executed before the try block return (arithmetic expression including a return statement) -> finally block is executed again, since the finally block there are return so early exit.
    5) Case 5: try {} catch () {return;} finally {return;}
      Program code execution (including expression operators return statement) before the return catch block -> then execute the finally block, the finally block because there is return so early exit.
    6) try {return;} catch () {return;} finally {return;}
      program executing on behalf of the previous try block return (arithmetic expression including a return statement) -> abnormalities: Before execution of the catch block return generation (including expression operators return statement) -> then execute the finally block, the finally block because there is return so early exit -> no exception: the re-implementation of the finally block, the finally block because there are so ahead of return drop out.

Guess you like

Origin www.cnblogs.com/yangrongkuan/p/12008534.html