Exception Handling in Java

Source: Internet
Author: User

Exception Handling class notes, hope to be useful to everyone.
Reprinted when please indicate the source: http://blog.csdn.net/javaeeteacher Thank you !!!!
1. What is exception handling?
Exception: some special cases during program running, such as: insufficient memory, file not found, object is Null, array subscript out of bounds. When these special situations occur, the program may not run properly.

Exception Handling: enables the program to run normally when an exception occurs.

2. Exception Classification
Check exception, which must be handled. if not handled, compilation fails.

Non-checked exceptions can be avoided, but once they occur, the program cannot continue to run.

3. Non-checked exceptions
Although no error is reported during compilation, it can be run. However, if an error occurs during running, the consequences are very serious. So we still need to handle it. You can add code to solve this problem.

Example: For NullPointerException, you can check whether the object is null before calling the method.

If (date! = Null ){

Date. getTime ();

}

Example: a/B (if B is 0, an exception occurs). Check whether B is 0 in the code.

If (B! = 0)

A/B;

Else

// Throw an exception!

You can also use try-catch for processing.

4. try... catch to handle exceptions

Preparation: Before Exception Handling, you must be clear about the exceptions that may occur. Exceptions are related to the methods to be called. Generally, you can declare possible exceptions through throws in the method definition.

Place the code that may generate exceptions in the try statement;

For each possible exception, write a catch and write the exception type in catch;

Compile the code for exception handling in the code corresponding to catch. When an exception is generated and matches the current exception type, the code in catch will be executed.

Basic Structure:

Try {

// Code that may generate exceptions

} Catch (exception type exception object ){

// Processing code when an exception occurs

}

 

Note:

Each type of exception must have a corresponding catch, and all exceptions should be handled.

You can use a parent class exception to match multiple subclass exceptions. Example: catch (Exception e ){}

If there is an inheritance relationship between the exception types, the subclass must be above.

If you want to use a variable in both try and catch, you should put the definition of the variable before the try statement.

 

Example:

Package ch11;

 

Import java. io. IOException;

 

Public class Test {

 

Public static void main (String [] args ){

String str1 = new String ("111a ");

Str1 = "22 ";

String str2 = "456 ";

// Write the code that may cause exceptions in the try statement

Try {

Int x = Integer. parseInt (str1 );

Int y = Integer. parseInt (str2 );

System. in. read ();

// When an exception occurs, the code after the Exception Code in the try statement is not executed

System. out. println (x + y );

// Return;

} Catch (NumberFormatException e) {// write Catch based on possible exceptions.

 

You can write one Catch for a type exception. You can use a parent class exception to match multiple subclass exceptions.

// If the exception types in multiple Catch statements have an inheritance relationship, the subclass should be written before

// If an exception of a certain type is matched, the code in catch will be executed.

System. out. println (e. getMessage ());

System. out. println ("---------------------------------");

System. out. println (e. toString ());

System. out. println ("---------------------------------");

E. printStackTrace ();

} Catch (IOException e ){

}

// If the try statement does not give a catch, it must be given finally, regardless of whether an exception occurs,

 

Code in finally will be executed. If catch is given, finally can also be given.

Finally {

System. out. println ("the program continues to run! ");

}

// System. out. println (new Test (). check ());

}

Public boolean check () throws IOException {

String str = "adasf ";

System. in. read ();

Int a = Integer. parseInt (str );

 

Return true;

}

 

}

 

5. Get exception information
When an exception occurs, the system will match a catch. The system will assign the exception object to the variables in the catch. The exception information can be accessed in the catch code block by using the following methods:

GetMessage () is the simplest description of an exception.

ToString () to obtain detailed exception information.

PrintStackTrace () outputs the exception information to the default output device.

For example:

Try {

// Code that may generate exceptions

} Catch (Exception e ){

System. out. println (e. toString ());

System. out. println (e. getMessage ());

E. printStackTrace ();

}

 

6. finally
If you want some code to be executed, whether or not an exception occurs, you can put the code in finally and use it with try,

 

Basic Structure

Try {

// Code block

} Catch (exception 1 e1 ){

} Catch (exception 2 e2 ){

} Finally {

// The code to be executed

}

 

Even if there is a return statement in try or catch, finally will certainly execute it before return.

 

7,

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.