Java Exception Handling Mechanism and try-catch-finally-return execution sequence

Source: Internet
Author: User

1. Simple Description: When an external environment problem that cannot be controlled by the program occurs (the file provided by the user does not exist, the file content is damaged, and the network is unavailable ...) JAVA will use exception objects to describe. 2. Use two methods to handle exceptions in JAVA: 1. directly handle exceptions in the case of exceptions; 2. Send exceptions to callers for processing. 3. JAVA exceptions can be divided into three types: (1) Check exceptions: java. lang. exception (2) runtime Exception: java. lang. runtimeException (3) error: java. lang. the top layer of Error is java. lang. throwable class, check exception, runtime exception, error is the Child class of this class. Java. lang. exception and java. lang. error inherited from java. lang. throwable, while java. lang. runtimeException is inherited from java. lang. exception. checksexual exception ------ the program is correct, but the cause is that the external environment conditions are not met. For example: user errors and I/O problems-the Program tries to open a remote Socket port that does not exist. This is not a logic error of the program itself, but probably a remote machine name error (User spelling error ). For commercial software systems, program developers must consider and handle this issue. The JAVA compiler enforces the processing of such exceptions. If such exceptions are not caught, the program cannot be compiled. Runtime exception ------ this means that the program has bugs, such as array out-of-bounds, 0 is excluded, and the input parameter does not meet the specifications ..... this type of exception must be avoided by changing the program. The JAVA compiler requires that such exceptions be handled. Error ------ it is generally rare and difficult to solve through a program. It may be due to program bugs, but it is generally due to environmental problems, such as memory depletion. Errors do not need to be processed in the program, but there is a runtime environment for processing. 4. try-catch-finally-return: 1. try-catch-finally: ①, run try {} block ②. If an exception occurs in the try {} block, run catch {} block ③. Execute finally {} block no matter whether there is any exception, here we can see that as long as there is return in finally, return 2, return execution in finally {} will be returned. ①. If there is a return statement in the finally {} block, only return Statement ② in block finally {} is executed. If there is no return statement in block finally {}, if try {} has an exception, return the return statement in catch, otherwise, execute the return statement in try {}. In this case, the return Statement block is executed after finally {} and has return: ① if try is normal/or abnormal, then run returnpackage com in finally. testjava. exception; public class TryCatchfFinally {public static void main (String [] args) {TryCatchfFinally tcf = new TryCatchfFinally (); System. out. println (tcf. testException ();} public String testException () {try {System. out. println ("start try"); int [] a = {2}; for (int I = 0; I <1; I ++) {// I = 2 ① System. out. println (a [I]);} System. out. println ("try end"); return "return of try not finally";} catch (ArrayIndexOutOfBoundsException e) {System. out. println ("arrayIndexOutOfBoundsException"); return "return in catch arrayIndexOutOfBoundsException";} catch (NullPointerException e) {System. out. println ("nullPointerException"); return "return in catch";} finally {System. out. println ("finally start try"); int [] a = {2}; for (int I = 0; I <1; I ++) {// I = 2 ② System. out. println (a [I]);} System. out. println ("finallyend end"); System. out. println ("finally"); return "return of finally" ;}}① I = 1 running result: start try2try endfinally start try2finallyend endfinallyreturn of finally ① I = 2 running result: start transaction start try2finallyend end endfinallyreturn of finally finished if no return exists in finally {}, if try {} is normal, run the try {} return statement. If an exception occurs, run the return statement in catch {}, but the return statement returns package com after the finally {} statement. testjava. exception; public class TryCatchfFinally {public static void main (String [] args) {TryCatchfFinally tcf = new TryCatchfFinally (); System. out. println (tcf. testException ();} public String testException () {try {System. out. println ("start try"); int [] a = {2}; for (int I = 0; I <1; I ++) {// ① System. out. println (a [I]);} System. out. println ("try end"); return "return of try not finally";} catch (ArrayIndexOutOfBoundsException e) {System. out. println ("arrayIndexOutOfBoundsException"); return "return in catch arrayIndexOutOfBoundsException";} catch (NullPointerException e) {System. out. println ("nullPointerException"); return "return in catch";} finally {System. out. println ("finally start try"); int [] a = {2}; for (int I = 0; I <1; I ++) {// ② System. out. println (a [I]);} www.2cto. comSystem. out. println ("finallyend end"); System. out. println ("finally"); // return "return of finally";} // return "end fuction" ;}}① I = 1 running result: start try2try endfinally start try2finallyend endfinallyreturn of try not finally ① I = 2 running result: start retry start try2finallyend end endfinallyreturn in catch arrayIndexOutOfBoundsException

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.