Java Basics-Exceptions

Source: Internet
Author: User

Statement: The material used in this column is written by the VIP students of the Kay Academy, who has the right to be anonymous and has the final right to interpret the article; The Edith Academy is designed to promote students to learn from each other on the basis of public notes.

Exception handling:

Can dig a lot of traps, but not all the same trap, otherwise meaningless.
Once an exception occurs, an exception object is generated, immediately terminating the code down, looking for a trap exception, and if the current trap cannot accept the exception object, it will be thrown out and thrown to the trap that can handle the exception.

Exception capture:
Exception trapping is a special processing mechanism that is not the same as if else. If else is a conditional judgment to branch, the exception once the occurrence of the termination code to run, immediately to find the capture area (processor) for the corresponding processing.

Capture Syntax: try{
Code that may be faulted
}catch (trap type) {
Processing
}

Example 1: The printed result is 1, 2, 3, 5, 6.
S.tostring (); There was an exception that jumped directly into the NullPointerException e trap, terminating the code number 4th, because the s.tostring error was thrown directly to the trap. After entering the trap, the corresponding processing is carried out and the code goes down again.

Example 2:
Printing results: 1, 2, 5, 7.
Execution process: Starting from the Main method, printed "1", "2", an error cannot be divisible by 0, thrown to the ArithmeticException e trap, printed "5", "7".

Example 3:
Printing results: 1, 2, 1, 3, 6, 7.
Execution process: First print 1, then Print 2, 1/1=1, print 1, then print 3, finally apply an array, the array length is 1, an array subscript out of bounds error occurred, into the (arrayindexoutofboundsexception e) trap, into the trapped after printing 6, Finally 7 were printed.

Example 4:
Printing results: 1, 2, 3.
Execution process: First printed 1, 2, 3, and then there is a subscript out of bounds error, the code does not dig down the error trap, it is not executable. Java principle: Once thrown, the exception will be crazy to find the corresponding exception of the trap, can not find the outside to find, terminate the code execution outside, outside in the search for the most outside. Will cause the code to terminate forever.

Example 5: First executes the Main method, called M2 (), M2 calls the M1,m1 call M,m there is an subscript out of bounds exception, m method does not handle the trap of the array out of bounds, so the array out of bounds can not handle the exception. Unable to deal with the outward throw, thrown to the callee, M1 no handlers, and throw to the m2,m2 and throw to the main method, the main method and the corresponding trap processing, jump to the array of cross-border processing traps, down execution. (If the main method does not have a corresponding trap, it will be thrown out)

Common pitfalls:
Throw exception is the object form, the object to find a trap when the use of instanceof can pass.
The common pitfall is to write their parent class, use the parent class as the trap type, and accept more exceptions.

Exception family: Object--------Throwable
1.Exepion (Program-thrown exception): RuntimeException (Run-time exception)
2.Error (Error thrown by virtual machine)

Exepion: is a more serious mistake, otherwise write a try () catch{} to serve, or throw it out. RuntimeException: This error is an operation error, which is generally considered to be avoidable if the technology is well experienced. So there is no need to force write try () catch{}.

Common traps are to be dug in the end, otherwise they will be unique. (otherwise meaningless)

Finally: the final execution block.
Written at the end.
An exception occurs, or a finally final execution block is executed if no exception occurs.

Writing:
1.try () [
}finally{
}

2.try{
}catch () {
}catch () {
}finally{
}

Examples:
Printing results: 1, 2, 4.
Execution process: First print 1, 2, and then appear not to be divisible by 0 exception, thrown outward, the final execution block to be executed before throwing out.

Example 2:
Printing results:
Execute the process: Print 1, 2, then hair an array out of bounds error, execute 4, and then throw to 6. Because the structure of the try is finished, the code inside is executed. Then the exception is thrown to 6, printing 6, then printing 7, 8.

Java Basics-Exceptions

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.