In the java try {} catch () {} explain

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https: //blog.csdn.net/qq_38225558/article/details/82054100
 

try {
     exception code (including not appear abnormal code) may occur
} catch (Exception e) {// () in try {} is the received code block type of exception occurs
     handling method if an exception occurs
}

try-catch order of execution:

① {} block of the first line of code is executed from try, to execute the code of abnormal, creates an exception object jvm
② is determined whether the type of exception catch declared to capture the exception object created jvm
       i: to capture: jump directly to the catch block execution. Does not end the program continues in the following catch block code execution
      II: not capture: information and terminates the direct print program exception
③try {} are without any exception, all the code in try executing the skip program continues to catch

note:

1. The catch can be captured condition == "consistent type as the declared catch the exception object try to create, or catch declared type of a subclass.

2. If only one catch catch only the specified type of exception object corresponding to the type of catch. If you try to create other types of abnormalities not catch. Jvm to handle exceptions

3.try {} block exists in the case of multiple lines of code throws an exception, the first encounters an exception, the exception will catch behind the code is captured but the abnormality information will not be executed

4. So how can we deal with try {} each abnormal it? ?

  A plurality of write catch ①: An abnormality processing each catch ==> try {} catch () {} catch () {} catch () {}
       effect: when the abnormality occurs in the try, skip to a catch at the judge, whether he can capture.
            a: capture: direct process. Then skip the next execution trycatch
            B: not capture: a catch continues to determine the next jump. If the next capture, repeat the first step of effect
            c: All catch can not catch ==> jvm to deal with: the end of the program, print exception information in console
  ② required to write a catch: catch the exception types must try parent class for all exceptions thrown
        
    Note: when writing more catch, catch declared exception types below can not be a subclass of catch declared the top.
                      When multiple catch, abnormal parent must be in the bottom. (When there is no inheritance relationship, do not take this into account)

try {} catch () {} catch () {} ... finally {} (catch can have more, or may not == "try {} finally {})

Features: finally write the code in the code block, it will be enforced to
note: ① the implementation of the return will be executed finally
           ② but the execution System.exit (0) will not execute finally up! ! !

ex1: (case Integer in = new Integer ( "1"); the abnormality does not occur)

 

ex2: (case Integer in = new Integer ( "A1"); the abnormal case)

 

ex3: multiple catch

 

ex4: 1 Ge catch

 

ex5: return does not affect the execution of the code finally {} block

 

ex6: performing System.exit (0) == "will no longer perform finally {} block code


----------------
Disclaimer: This article is the original article CSDN bloggers "Zheng Qing", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/qq_38225558/article/details/82054100

Guess you like

Origin www.cnblogs.com/moegarn/p/11565822.html