Java Exception Handling

Source: Internet
Author: User

 Exception handling

Java exception handling is managed by 5 keyword try, catch, throw, throws, finally. The basic process is to wrap the statement that you want to monitor with a try statement block, and if an exception occurs within a try statement block, the exception is thrown, your code can catch the exception in the Catch statement block, and the exception that is generated by some system is automatically thrown in the Java runtime. You can also declare the method to throw an exception by using the throws keyword on the method, and then throw the exception object through a throw inside the method. The finally statement block executes before the method executes a return,

The code is as follows:

try{

Program code

}catch (Exception type 1 exception variable name 1) {

Program code

}catch (Exception type 2 exception variable name 2) {

Program code

}finally{

Program code

}

A catch statement can have more than one exception, which matches more than one of the above, and executes the CATCH statement block only when the exception on the match is executed. The type of catch is defined in the Java language or is defined by the programmer itself, indicating that the code throws an exception, the exception variable name represents the reference to the object that throws the exception, and if the catch catches and matches the exception, then the exception variable name can be used directly. At this point the exception variable name points to the matching exception and can be referenced directly in the catch code block.

Java Exception handling mechanism

First, in the method to catch and handle the exception with the Try...catch statement, the Catach statement can have multiple, to match multiple exceptions.

Second, for exceptions that cannot be handled or exceptions to be transformed, an exception is thrown through the throws statement at the declaration of the method.

Syntax rules for exception handling

The first, try statement can not exist alone, and catch, finally make up try...catch...finally, Try...catch, try...finally Three kinds of structure, catch statement can have one or more, The finally statement has at most one, and the try, catch, finally three keywords cannot be used alone.

The scope of the variables in the second, try, catch, finally three blocks of code is independent and cannot be accessed from one another. If you want to be accessible in three blocks, you need to define the variables outside of those blocks.

Third, multiple catch blocks, when a Java virtual opportunity matches one of the exception classes or its subclasses, executes the catch block without executing another catch block.

The throw statement is not allowed to follow the other statements, since these do not have a chance to execute.

If a method invokes another method that declares an exception, the method either handles the exception or throws the declaration.

The difference between the throw and throws keywords

Throw is used to throw an exception in the method body. The syntax format is: Throw exception object.

Throws is used to declare what exceptions the method might throw, after the method name, the syntax format is: throws exception type 1, exception type 2 ... Exception type N.

Java Exception Handling

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.