Exception-note-01,-note-01

Source: Internet
Author: User

Exception-note-01,-note-01

1. Use try-catch to catch a single exception

If an exception occurs, the program will be terminated immediately. We have to handle the exception:

1): This method is not processed, but declared and thrown, which is handled by the caller of this method (throws)

2): Use the try-catch statement block in the method to handle exceptions-

Use try-catch to catch a single exception:

Syntax:

Try {

Code that may cause exceptions

} Catch (exception type e ){

Code for handling exceptions

// Record logs/print daily information and continue to throw an exception

}

Note:: Catch and try cannot be used separately.

 

 

2. Obtain and view exception information

1): String getMessage (): gets the description of the exception and the cause.

2) String toString (): obtains the exception type and description (not required)

3): void printStackTrace (): prints abnormal trace stack information and outputs it to the console. You do not need to use System

 

 

Contains the exception type, cause, and location. printStackTrace is required during development and debugging.

In the catch Block, you must enter e. printStackTrace (). Purpose: To view the exception details for debugging and modification.

 

3. Use try-catch to catch multiple exceptions

Try {

Code that may cause exceptions

} Catch (Exception typeAE) {when A type exception occurs in try, use the catch to capture

Code 1 for handling exceptions

// Record logs/print daily information and continue to throw an exception

} Catch (Exception typeBE) {when a type B exception occurs in try, use the catch to capture

Code 2 for Exception Handling

// Record logs/print daily information and continue to throw an exception

}

Note:Only one type of exception can occur in an instant in the Code. Only one catch is required, and multiple exceptions cannot occur at the same time.

Put Exception at the end

 

 

4. Ensure that the finally code block of the resource is disabled

The finally statement block indicates the code that will be executed eventually, no matter whether there is any exception.

When must the code be finally executed?

When we open some physical resources (Disk Files, network connections, database connections, etc.) in the try statement block, we have to close the opened resources after use,

Finally syntax

1): try... finally: No catch to catch exceptions, because at this time, based on the application scenario, we will throw an exception and will not handle it ourselves.

2): try... catch... finally: You need to handle exceptions and close the resources.

Note: finally cannot be used independently.

 

Finally does not execute this function only when the jvm exit method is called in try or catch,

Otherwise, finally will always Execute

System. exit (0): // exit jvm

Bytes --------------------------------------------------------------------------------------------

RequiredUse finally to ensure that the final code must be executed

 

For exampleIf finally has a return statement, the finally result is always returned to avoid this situation.,

 

6. Exception Classification

1): exception during the compilation period and checked exception. During the compilation period, the system checks. If no exception is handled, the compilation fails.

2): runtime exception, runtime exception, during runtime, check exception, during compilation, run exception will not be detected by the compiler (no error is reported)

Running exception: during compilation, it can be processed, but not handled.

How to determine whether a class is compiled or run abnormally

Returned error result-throw statement

Throw an exception:

Throw: used inside a method to return an exception object to the caller. Like returm, throw ends the current method.

Throws: used on the method declaration to indicate that the current method does not handle exceptions, but reminds the caller of the method to handle exceptions (

(Throw an exception)

// Indicates that this method does not handle certain types of exceptions and reminds the caller to handle exceptions.

For example:Private Static IntDivide (IntNum1,IntNum2)ThrowsException

If every method does not handle the exception, Are directly thrown through the throws declaration, and the final exception will be thrown to the main method.

If the mian method is not processed at this time, it will continue to be thrown to the jvm. The underlying processing mechanism is to print the tracing stack information for exceptions.

Runtime exception. This is the default processing method.

 

 

Throw statement:

When used inside the method,Specific exception object,

Throw new exception class ("exception information"); termination method.

Throw:

In general, when a method is abnormal, we do not know what the method should return. In this case, an error is returned,

Continue to throw an exception in the catch statement block.

Return indicates that a value is returned, and throw indicates that an error is returned to the caller of the method.

 

Analyze and understand the design details of each module in the framework at a micro level

Macro Analysis and overall design of the control framework

 

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.