12-21java object-oriented exception

Source: Internet
Author: User

12-21java object-oriented exception
1. Exception

An exception is a type of command flow that causes program interruption.

Public class TestException1 {public static void main (String [] args) {int I = 10; // defines the integer variable int j = 0; // define the integer variable int temp = I/j; System. out. println ("result of division of two numbers:" + temp );}}

An exception occurs after the program is executed:

F: \ Reliability \ java \ work \ Exception> java TestException1

Exception in thread "main" java. lang. ArithmeticException:/by zero

At TestException1.main (TestException1.java: 7)

Public class TestException1 {public static void main (String [] args) {System. out. println ("***********************"); int I = 10; // define the integer variable int j = 0; // define the integer variable int temp = I/j; // an exception is reported here. out. println ("result of division of two numbers:" + temp); System. out. println **************");}}

The execution results show that the execution is started but not completed. Once an exception occurs, the program after the exception stops executing but ends the program directly and reports the error to the user.

F: \ Reliability \ java \ work \ Exception> java TestException1

**************

Exception in thread "main" java. lang. ArithmeticException:/by zero

At TestException1.main (TestException1.java: 8)

Two major computer Killers:

1. Power Failure

2. The divisor is 0 --> the memory is insufficient. All are full.

1.1 Exception Handling

To handle exceptions, you must use the Exception Handling Format:

Try --> catch --> finally

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel/E8L3A + CjxwcmUgY2xhc3M9 "brush: java;"> public class TestException2 {public static void main (String [] args) {System. out. println ("***********************"); int I = 10; // define the integer variable int j = 0; // define the integer variable try {int temp = I/j; System. out. println ("result of division of two numbers:" + temp); System. out. println ("_____________________"); // two statements are not executed} catch (ArithmeticException e) // an arithmetic exception {System. out. println ("exception:" + e);} System. out. println **************");}}

Execution result:

For exceptions, you can set the uniform exit finally.

Public class TestException3 {public static void main (String [] args) {System. out. println ("***********************"); int I = 10; // define the integer variable int j = 0; // define the integer variable try {int temp = I/j; System. out. println ("result of division of two numbers:" + temp); System. out. println ("_____________________");} catch (ArithmeticException e) // arithmetic exception {System. out. println ("exception:" + e);} Finally // The unified exit of the exception {System. out. println **************");}}}

The above program only handles one exception in the Code. If there are multiple exceptions?

Input the value of I and j through initialization parameters

Public class TestException4 {public static void main (String [] args) {System. out. println ("**********************"); try {int I = Integer. parseInt (args [0]); // initialization parameter int j = Integer. parseInt (args [1]); int temp = I/j; System. out. println ("result of division of two numbers:" + temp); System. out. println ("_____________________");} catch (ArithmeticException e) // arithmetic exception {System. out. println ("exception:" + e);} finally {System. out. println **************");}}}

Different results are obtained for different inputs.

Modify the catch code for the above problems.

Public class TestException4 {public static void main (String [] args) {System. out. println ("**********************"); try {int I = Integer. parseInt (args [0]); // initialization parameter int j = Integer. parseInt (args [1]); int temp = I/j; System. out. println ("result of division of two numbers:" + temp); System. out. println ("_____________________");} catch (ArithmeticException e) // arithmetic exception {System. out. println ("exception:" + e);} catch (ArrayIndexOutOfBoundsException e) // array out-of-bounds exception {System. out. println ("exception:" + e);} catch (NumberFormatException e) // numeric conversion format exception {System. out. println ("exception:" + e);} finally {System. out. println **************");}}}

The above program needs to handle three exceptions, which makes it very troublesome for the program to handle exceptions, so we have a deep understanding of the inheritance structure of exception classes.

1.2 inheritance results of exception classes

Three previous exceptions: ArithmeticException

ArrayIndexOutOfBoundsException

NumberFormatException

Exception Handling in Jav. lang-> class has a class.

There are two most common classes in java exception Handling: exception and error.

Exception indicates a problem in the program. try-catch

Eoor indicates an error in the JVM and cannot be processed by the program.

Note: When outputting Exception information, you can directly use System. out. println () for output, or use the public void printStackTrace () method in the Exception class to print Exception information.

1.3java Exception Handling Mechanism

Throughout java, exception handling is implemented through object-oriented installation.

1. Once an exception occurs, an instantiated object of the exception class will be generated first.

2. Capture with try statements

3. The generated exception object matches the catch exception types.

I have explained the object polymorphism before. The instantiation of child classes can be received by objects of the parent class. This concept is actually used in exception handling because try produces instantiated objects, can be upgraded.

If some exceptions are unknown, use exception to capture them. At this time, the exception is placed at the end.

Public class TestException5 {public static void main (String [] args) {System. out. println ("**********************"); try {int I = Integer. parseInt (args [0]); // initialization parameter int j = Integer. parseInt (args [1]); int temp = I/j; System. out. println ("result of division of two numbers:" + temp); System. out. println ("_____________________");} catch (Exception e) {System. out. println ("rough exception capture" + e);} catch (ArithmeticException e) // arithmetic exception {// System. out. println ("exception:" + e); e. printStackTrace ();} catch (ArrayIndexOutOfBoundsException e) {System. out. println ("exception:" + e);} catch (NumberFormatException e) {System. out. println ("exception:" + e);} finally {System. out. println **************");}}}
Error Message

TestException5.java: 18: Error: Caught Exception error ArithmeticException

Catch (ArithmeticException e)

^

TestException5.java: 23: Error: Caught Exception error ArrayIndexOutOfBoundsException

Catch (ArrayIndexOutOfBoundsException e)

^

TestException5.java: 27: Error: Caught Exception error NumberFormatException

Catch (NumberFormatException e)

^

The program is executed in sequence. The coarse content is captured first, so it is not executed in detail.

Since all the Exception objects can be transformed upwards, it is more convenient to directly use Exception for capturing.

Public class TestException6 {public static void main (String [] args) {System. out. println ("**********************"); try {int I = Integer. parseInt (args [0]); // initialization parameter int j = Integer. parseInt (args [1]); int temp = I/j; System. out. println ("result of division of two numbers:" + temp); System. out. println ("_____________________");} catch (Exception e) {System. out. println ("rough exception capture" + e);} finally {System. out. println **************");}}}

When all exceptions are handled in the same way, the above methods can be used. We do not recommend this method in more detailed development.

Since exception capture is convenient, is it better to capture throwable directly?

First, throwable is the parent class of exception, which is theoretically correct. However, the program try will only throw the exception object, but the error does not. Therefore, this method is not used.

1.4 conclusion

1. The program stops when an exception occurs.

2. Use try-catch-finally

3. Multiple catch captures can be placed at the end at the same time. Otherwise, the program compilation may fail.

4. The largest class is throwable, which is divided into two subclasses.









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.