12-21java Object-oriented exceptions

Source: Internet
Author: User

1. Exceptions

An exception is a flow of instruction that causes a program to break execution.

public class TestException1 {public static void main (string[] args) {int i = 10;//define integer variable int j = 0;//define integer variable int temp = i/j; SYSTEM.OUT.PRINTLN (the result of dividing two numbers: "+ temp);}}

An exception prompt appears after the program executes:

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 ("*********** program starts ************** int i = 10;//defines an integer variable int j = 0;//define integer variable int temp = i/j;     This produces the exception System.out.println (the result of dividing two numbers: "+ temp); SYSTEM.OUT.PRINTLN ("*********** program End **************");}}

The results of the execution were found, the execution started but the end was not executed. Once an exception is generated, the program that follows the exception is no longer executed but ends the program and reports the error to the user.

F:\reliability\java\work\exception>java TestException1

program starts **************

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

At Testexception1.main (Testexception1.java:8)

Two major killers of computers:

1. Power failure

2. The divisor is 0--> Insufficient memory, full

1.1Handling of exceptions

To handle exceptions, you must use the exception handling format:

Try-->catch-->finally

To capture a previous exception

public class TestException2 {public static void main (string[] args) {System.out.println ("*********** program starts ************** int i = 10;//defines an integer variable int j = 0;//define integer variable Try{int temp = i/j; SYSTEM.OUT.PRINTLN (the result of dividing two numbers: "+ temp); System.out.println ("_____________________");     Two statements did not execute}catch (arithmeticexception e)//Arithmetic exception {SYSTEM.OUT.PRINTLN ("Exception occurred:" + e);} SYSTEM.OUT.PRINTLN ("*********** program End **************");}}

Execution Result:

For exceptions, you can set up a unified export finally finish.

public class TestException3 {public static void main (string[] args) {System.out.println ("*********** program starts ************** int i = 10;//defines an integer variable int j = 0;//define integer variable Try{int temp = i/j; SYSTEM.OUT.PRINTLN (the result of dividing two numbers: "+ temp); System.out.println ("_____________________");} catch (ArithmeticException E)//Arithmetic exception {SYSTEM.OUT.PRINTLN ("Exception occurred:" + e);} Finally   //exception of the unified export {System.out.println ("*********** program End **************");}}}

The above program only handles an exception in the code, if there are multiple exceptions?

Enter the value of i,J by initializing parameters

public class TestException4 {public static void main (string[] args) {System.out.println ("*********** program starts ************** "); Try{int i = Integer.parseint (args[0]);//initialization parameter Int j = integer.parseint (Args[1]); int temp = i/j; SYSTEM.OUT.PRINTLN (the result of dividing two numbers: "+ temp); System.out.println ("_____________________");} catch (ArithmeticException E)//Arithmetic exception {SYSTEM.OUT.PRINTLN ("Exception occurred:" + e);} FINALLY{SYSTEM.OUT.PRINTLN ("*********** program End **************");}}}

For different inputs, a different result is obtained.

For the above problems, modify the catch code

public class TestException4 {public static void main (string[] args) {System.out.println ("*********** program starts ************** "); Try{int i = Integer.parseint (args[0]);//initialization parameter Int j = integer.parseint (Args[1]); int temp = i/j; SYSTEM.OUT.PRINTLN (the result of dividing two numbers: "+ temp); System.out.println ("_____________________");} catch (ArithmeticException E)//Arithmetic exception {SYSTEM.OUT.PRINTLN ("Exception occurred:" + e);} catch (arrayindexoutofboundsexception E)    //array out of bounds exception {SYSTEM.OUT.PRINTLN ("Exception occurred:" + e);} catch (NumberFormatException E)     //Number conversion format exception {SYSTEM.OUT.PRINTLN ("Exception occurred:" + e);} FINALLY{SYSTEM.OUT.PRINTLN ("*********** program End **************");}}}

The above procedures need to handle three exceptions, so that the program exception handling is very cumbersome, so in-depth understanding of the inheritance structure of the exception class.

1.2inheritance result of exception class

Three exceptions that occurred before:arithmeticException

ArrayIndexOutOfBoundsException

NumberFormatException

There is a class for exception handling in exception in Jav.lang->class.

There are two most commonly used classes in Java exception Handling--exception and error classes

Where exception represents a problem in the program,try-catch processing

Eoor refers to a bug in the JVM that the program cannot handle

Note: When outputting exception information, you can use System.out.println () to output directly, or you can use The method public in the Exception class void Printstacktrace (), which specializes in printing exception information.

1.3javaexception handling mechanism

In the entire Java , the handling of exceptions is done by installing an object-oriented approach

1. Once an exception is generated, an instantiated object of the exception class is generated first

2. Capturing with a try statement

3, the resulting exception object and catch each exception type to match

Before explaining the polymorphism of the object, the instantiation of the subclass can be received with the object of the parent class, and in the exception handling, it is actually used, because the try produces an instantiated object that can be transformed upward.

If there are certain exceptions, then the exception is used to capture them. At this point the exception is put to the last.

public class Testexception5{public static void Main (string[] args) {System.out.println ("*********** program starts ************** "); Try{int i = Integer.parseint (args[0]);//initialization parameter Int j = integer.parseint (Args[1]); int temp = i/j; SYSTEM.OUT.PRINTLN (the result of dividing two numbers: "+ temp); System.out.println ("_____________________");} catch (Exception e) {System.out.println ("the most coarse exception capture" + E);} catch (ArithmeticException E)//Arithmetic exception {//SYSTEM.OUT.PRINTLN ("Exception occurred:" + e); E.printstacktrace ();} catch (ArrayIndexOutOfBoundsException e) {System.out.println ("Exception occurred:" + e);} catch (NumberFormatException e) {System.out.println ("Exception occurred:" + e);} FINALLY{SYSTEM.OUT.PRINTLN ("*********** program End **************");}}}
Error hints

Testexception5.java:18: error : An exception was caught in the error arithmeticexception

catch (ArithmeticException e)

^

Testexception5.java:23: error : An exception was caught in the error arrayindexoutofboundsexception

catch (ArrayIndexOutOfBoundsException e)

^

TESTEXCEPTION5.JAVA:27: error : An exception was caught in the error numberformatexception

catch (NumberFormatException e)

^

The order of the program executes, first captures the coarse, the fine does not have in the execution.

Since all Exception objects can be transformed upward, it is more convenient to capture them directly using Exception .

public class Testexception6{public static void Main (string[] args) {System.out.println ("*********** program starts ************** "); Try{int i = Integer.parseint (args[0]);//initialization parameter Int j = integer.parseint (Args[1]); int temp = i/j; SYSTEM.OUT.PRINTLN (the result of dividing two numbers: "+ temp); System.out.println ("_____________________");} catch (Exception e) {System.out.println ("the most coarse exception capture" + E);} FINALLY{SYSTEM.OUT.PRINTLN ("*********** program End **************");}}}

When all the exceptions are handled in the same manner as can be used in the form above, it is not recommended to use this in more detailed development.

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

First throwable is exception 's parent class, and theory is no problem. However , only exception objects are thrown in the program try , but the error is not. So do not use this method.

1.4Summary

1. Abnormal appearance will cause the program to stop

2. Using try-catch-finally

3. Multiple catch captures can occur at the end of the rough, or the program compiles a problem

4. The largest class is the throwable divided into two sub-classes









12-21java Object-oriented 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.