Java exception handling mechanism Java Architect Learning Java Beijing

Source: Internet
Author: User

in the Java, abnormal conditions are divided intoException(Exception) andError(Error) Two major categories,Javaexception usually refers to the program during the operation of abnormal conditions, such as user input errors, divisor is zero, need to deal with the file does not exist, array subscript out of bounds, for the occurrence of abnormal conditions, can use exception processing, in order to ensure that the program can be normal execution.

Java Two types of exceptions and errors are defined in:

1. JVM (Java virtual machine ) exception: an exception or an error thrown by the JVM. For example:NullPointerException class,arrayindexoutofboundsexception class,classcastexception class.

2. Program-level Exceptions: Exceptions thrown by programs or API programs. such as the IllegalArgumentException class, theillegalstateexception class.

Methods for catching exceptions

using the try and catch keywords to catch exceptions,the code in the Try/catch code block is called the protection Code and can be placed where the exception can occur, using the following:

Try

{

Program Code

}catch (Exceptionname E1)

{

Catch Block

}

The Catch statement contains the declaration to catch the exception type. When an exception occurs in the protection code block,the catch block after the try is checked. If the exception that occurs is contained in a catch block, the exception is passed to the catch block, which is the same as passing a parameter to the method. You can also make multiple captures by following multiple catch blocks behind a try code block .

A catch statement is often used in conjunctionwith Finally, and the finally keyword is used to create a block of code that executes after a try code block, whether or not an exception occurs,finally The code in the code block is always executed, so in the finally code block, you can run an end-of-the-loop statement like cleanup type.

Declaring custom exceptions

in the You can customize exceptions in Java, and you should be aware of the following points when customizing exception classes:

1. all exceptions must be sub-classes of throwable;

2. Custom Check exception classes, you need to inherit the Exception class;

3. Custom Run-time exception classes, you need to inherit the RuntimeException class.

The custom exception class is the same as any other class, and contains variables and methods. Its mechanism is to throw exceptions and catch exceptions, a method can catch the exception, it must be Java code in a certain place throws the exception, the exception is always first thrown, after being captured.


Java exception handling mechanism Java Architect Learning Java Beijing

Related Article

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.