Java Chapter Sixth: exception mechanism

Source: Internet
Author: User
Tags finally block

Exception mechanism

Is the exception event that occurs during the execution of the program, which causes the program to break.

Methods to solve the exception: use try,catch,finally,throw,throws five keywords to solve;

1.try...catch ... √

2.try...catch...catch ... √

3.try...catch...finally ... √

4.try...finally ... √

5.catch...finally ... Wrong wording

The statements in the finally block are always executed, often writing the code released by the resource, and a return will be executed, unless it encounters system.exit (1), which is not executed until it exits the JVM;

There is a return in the try-catch-fianlly, the order of execution: There is no exception match in the try block to the time, Try-finally-return; there is an exception match to Try-catch-finally-return, Anyway, finally is always executed before return.

Throws and throw keyword, is in need to throw an exception to the outside, only use, generally used together;

The difference between the two:

1. Declaration location is different: throws is the exception type that declares the method when declaring a method

Throw is an exception thrown inside a method

2. The following is not the same as: throws followed by the exception type, throw followed by, new exception object;

3. Different functions: Throws is to declare the possible exception type of this method; throw is to throw out the exception that occurs, and the external call needs to handle the exception.

Throwable is the parent class for error (Errors) and exception (exceptions): All under the Java.lang package

Exception is divided into:

Run-time Exception: Exception that occurs after run, do not force try: Catch captures, common are: nullpointexception, classcastexception, NumberFormatException, Arrayindexoutofbandexception, Inputmismatchexception, arithmetic exception

Check for exceptions: Must be caught or the compilation will not pass. Common are: sqlexception,ioexception,classnotfoundexception, etc.

Custom exception: The exception in the JDK does not meet the coding requirements.

Steps:

1. Customize an exception class, inheriting exception or runtimeexception;

2. In the custom exception class, create a constructor method that inherits the parent class super (String message)

3. Create a custom exception object where an exception may occur

4. Throw an exception with throw

Java Chapter Sixth: exception mechanism

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.