[Basic concepts of exceptions] _ notes on basic concepts of exceptions

Source: Internet
Author: User

[Basic concepts of exceptions] _ notes on basic concepts of exceptions

Understand basic concepts of exceptions
Master basic Exception Handling formats
Understanding the inheritance structure of exception classes
Master Java Exception Handling Mechanism

Exception:
An exception is a type of command flow that causes program interruption. If the exception is not correctly handled, the program may be interrupted and cause unnecessary losses.

3.1 why exception handling?

Public class exceptiondemo01 {public static void main (string ARGs []) {system. out. println ("***** computing start *******"); int I = 10; Int J = 0; int temp = I/J; // an exception is thrown here: system. out. println ("result of division of two numbers:" + temp); system. out. println ("****** computing end ****");}}

Once an exception occurs, the statement will not be executed. Instead, the program is directly ended and the error is reported to the user.
There are two major killers in the history of computer development:
1. Power Failure
2. The divisor is 0.
3.2 handling exceptions

If you want to handle exceptions, you must master the Exception Processing format.

Try {

} Catch (){

} Finally {

}
Capture previous exceptions

Public class exceptiondemo02 {public static void main (string ARGs []) {system. out. println ("******** computing start *********"); int I = 10; Int J = 0; try {int temp = I/J; // exception system is generated here. out. println ("result of division of two numbers:" + temp); system. out. println ("------------------")} catch (arithmeticexception e) {e. printlnstacktrace ();} finally {system. out. println ("whether or not an exception occurs, execute this Code");} system. out. println ("****** computing end *******");}}

At this time, the data is actually handed over to the user for input. Since it is user input, the input may be incorrect. In fact, the above Code has three problems:
1. If no parameter is entered or the input parameter is insufficient, the following error occurs:
Java. Lang. arrayindexofboundsexception

2. If the input parameter is not a number but a letter.
Java. Lang. numberformatexception: for input string: ""

3. An Arithmetic exception occurs. If the divisor is zero.

Therefore, the program actually generates three exceptions:
Array exceeded binding: arrayindexoutofboundsexception
Number formatting error: numberformatexception
Arithmetic exception: arithmeticexception

If you want to ensure that the program runs correctly, you must handle three exceptions at the same time. Therefore, you need three catch statements.

Public class exceptiondemo05 {public static void main (string [] ARGs) {system. out. println ("***** computing start *******"); int I = 0; Int J = 0; try {string str1 = ARGs [0]; string str2 = ARGs [1]; I = integer. parseint (str1); j = integer. parseint (str2); int temp = I/J; // an exception is generated here. out. println ("result of division of two numbers:" + temp); system. out. println ("-----------------------");} catch (arithmeticexception e) {e. printstacktrace ();} catch (numberformatexception e) {e. printstacktrace ();} catch (arrayindexoutofboundsexception e) {e. printstacktrace ();} system. out. println ("****** computing end ******");}}

The above small program must handle three exceptions. Of course, this program may have other exceptions, so it is very troublesome to handle exceptions.

3.3 abnormal inheritance Structure

In the exception structure of Java, there are actually the following two most common classes: exception and error, both of which are subclasses of throwable.
1. Exception: Generally, it indicates a problem in the program. You can use try... catch to handle it directly.
2. Error: generally refers to a JVM error that cannot be processed in the program.

Generally, developers are used to calling error and exception together as exceptions. Some exceptions previously handled are subclasses of exception.
Note:
When outputting exception information, you can directly use system. Out. println () to print the exception object.
You can also use the method provided by exception: Public void printstacktrace ()

3.4 Exception Handling Mechanism:

Try {

} Catch (arithmeticexception e ){

} Finally {

}

Exception can be captured directly when all Exception Handling methods are the same. Of course, this is not recommended in a more detailed development, it is best to capture all exceptions separately.

Problem:
Since exception capture is the most convenient, can throwable be captured directly?
First, this method is acceptable, but normal developers will not do this, because the try of the program will always only throw the exception subclass, throwable contains not only exceptions but also errors.

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.