Java exception handling experience and exception handling experience

Source: Internet
Author: User

Java exception handling experience and exception handling experience

There are always various problems in the program. To enable normal operation during program execution, use the exception handling mechanism provided by Java to capture Possible exceptions, handle exceptions and make the program run properly. This is Java exception handling.

I. Caught exceptions

Exceptions that can be captured in Java are classified into controllable and runtime exceptions.

1. Controllable exceptions

In Java, unexpected errors can be processed during program compilation, and specific error information is provided. These errors are called controllable errors. Common controllable exceptions are as follows:

Exception indicates IOException. When an I/O exception occurs, throw this exception SQLException. The ClassNotFoundException class for database access errors or other error messages does not find the exception. NoSuchFieldException the signal generated when the NoSuchMethodException class does not contain fields with the specified name cannot find a specific method., throw this exception

2. runtime exception

Errors that cannot be detected by the compiler in Java are called runtime exceptions. Common runtime exceptions are as follows:

An exception occurs when IndexOutOfBoundsException indicates that the index value of a collection or array is out of the range. If the application tries to use null where the object is needed, this ArithmeticException is thrown. When an abnormal operation condition occurs, if this exception is thrown, IllegalArgumentException indicates that an invalid or incorrect ClassCastException parameter is passed to the method. This exception is thrown when an attempt is made to forcibly convert an object to a subclass other than an instance.

Ii. Handling exceptions

In Java, when an exception occurs in a program, try · catch, try · catch · finally or try · finally can be used for processing.

1. Use try · catch to handle exceptions

Try is a normal execution statement, while catch is a statement for exception handling. catch is the exception type that the program needs to handle in parentheses. The syntax format is as follows:

Try {statements executed normally} catch (Exception e) {statements for Exception handling}

Here is an example of an arithmetic exception.

Public class ExceptionTest {public static void main (String [] args) {int result = 1/0; try {System. out. println (result);} catch (Exception e) {System. out. println ("throw an exception:" + e. getMessage ());}}}

Here, 1/0 is an exception algorithm, because the divisor cannot be 0. The running result is as follows:

Because of exceptions, do not execute the try statement to execute the catch statement. "E. getMessage () "is a method for obtaining exception information. It is used to obtain detailed message strings. In addition, it is also used to output stack traces to standard error streams. toString () method To Get A brief description.

2. Use try · catch · finally to handle exceptions

Here, the try and catch statements are the same as the previous ones, and the finally statements are executed no matter whether an exception occurs or not. Therefore, the finally statement block is usually used for garbage collection. The syntax format is as follows:

Try {normal execution statement} catch (Exception e) {statement for Exception handling} finally {statement to be processed}

3. Use try · finally to handle exceptions

When a program encounters an exception, you can perform corresponding processing on it in the finally statement block. In addition, when the program does not have an exception, after the statements between try and finally are executed, the finally statement block code will also be executed. The syntax format is as follows:

Try {statements to be executed} finally {statements to be processed}

Iii. Throw an exception

In addition to the above try catch statement, you can also use throws declaration or throws statement to throw exceptions.

1. Throw an exception using throws Declaration

Throws is used for method declaration. When declaring a method, throws is used to declare and throw an exception. Then, an exception is handled when the method is called.

To declare multiple exceptions, use commas to separate them. The syntax format is as follows:

Data Type method name (form parameter list) throws exception class 1, exception class 2 ,......, Exception class n {method body ;}

For example, throws is used to throw an Exception.

Public void showInfo () throws Exception {// throw Exception FileInputStream in = new FileInputStream ("C: // Record.txt"); // creates an IO object}

2. throw an exception using the throw statement

If you want the program to throw an exception on its own, you can use the throw statement. Syntax format: throw new Exception ("Exception description ");

An instance of the exception class is thrown using the throw Statement, which is usually used together with the if statement. For example:

If (x <0) {throw new Exception ("program Exception, x cannot be less than 0. ");}

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.