Java- abnormal 01

1. Abnormal

    In Java, the exception information package into a class. When there is a problem, it will create an exception class object and throw an exception related information (such as abnormal position, causes, etc.). 

In using the Java Exception class to describe abnormal.

See API described in the Exception, Exception class and its subclasses are Throwable a form, which is used to represent the java program exception may occur, and requires reasonable exception handling abnormality generated.

 Exception inheritance relationship, it is the parent class Throwable. Throwable Java language is all wrong or unusual super-categories, namely class ancestors.

In abnormal Exception class, there is a special sub-class to explain, RuntimeException subclass , RuntimeException and other subclasses can only occur in the course of running a Java program.

 

Abnormal Exception same level there is a Error , it is Throwable subclass, which is used to indicate a serious error in java program may be generated. Only one solution, modify the code to avoid Error errors.

to sum up:

Throwable: it is all wrong and abnormal superclass (ancestor class)

| - Error Error

| - Exception compile-time exception occurs when compiling JAVA procedures

       | - RuntimeException runtime exception, the process JAVA program running emerging issues

 

The difference between anomalies and errors

 Abnormal : refers to an abnormality has occurred in the program, during the compilation run (XxxException), we can carry out specific abnormal processing. If not handle the exception, the program will end run.

 

Error : some sort of error refers to the program occurred during operation (XxxError), Error error is usually no specific treatment program will be terminated run. Error error occurs are often system-level problems are jvm where the system occurred, and feedback to the jvm. For we can not handle, it can only be corrected code.

 

Throw an exception throw

 

When defining process, process parameters need to accept. So, when you call the method using the parameters received, first of all need to determine the legal parameters of the data, the data if legitimate, you should tell the caller, transfer the data to come in legally. Then you need to use throwing an exception to tell the caller.

In java, there is provided a throw keyword, which is used to throw an exception object specified

To do:

1, create an exception object. Package some tips (information can write your own).

 2, this exception object needs to be informed to the caller. How to inform it? How this exception object passed to the caller at it? By keyword throw to complete. throw exception objects;

throw used in the method, the object to throw an exception, the exception pass the object to the caller, and ends the execution of the current method.

Using the format :

throw new exception class name (parameter);

E.g:

throw new NullPointerException ( "arr array to access does not exist");

throw new ArrayIndexOutOfBoundsException ( "the index does not exist in the array, it has been out of range");

Code demonstrates:

 Writing tools, providing access to an array element values ​​at the specified index

class ArrayTools{

// through a given array, return the value of the element corresponding to the given index.

public static int getElement(int[] arr,int index) {

/*

If the program out of an exception, the JVM it will be packaged exception object and throws. But the information it provides is not enough to force. Want more clearly, the information you need to throw an exception.

Determining if the following conditions are satisfied, when executing the post-throw thrown objects, methods have been unable to continue operation. Then it will end the execution of the current method, and abnormal informed to the caller. Then you need to be solved by exception.

*/

if(arr==null){

throw new NullPointerException ( "arr directed array does not exist");

}

if(index<0 || index>=arr.length){

throw new ArrayIndexOutOfBoundsException ( "standard error angle," + index + "does not exist in the index array");

}

int element = arr[index];

return element;

}

}

 Test category

class ExceptionDemo3 {

public static void main(String[] args) {

int [] arr = {34,12,67}; // create an array

= ArrayTools.getElement (null, 2) int num; // call the method, obtaining the array element at the specified index

// int num = ArrayTools.getElement (arr, 5); // call the method, obtaining the array element at the specified index

System.out.println ( "num =" + num); // print element values ​​obtained

}

}

 

Statement throws an exception

 

Disclaimer: The identity issue out of the report to the caller. If in the method by abnormal, but not capture processing throw thrown compilation (later to explain the way), it must be declared by throws, let the caller to deal with.

Exception declarations Format: Modifier return type method name (parameter) throws exception classes 1, 2 ... exception classes {}

Code demonstrates:

class Demo{

 

/*

 

If there is a problem to be reported to the caller when defining the function. Can be used on a method throws keyword statement.

 

*/

 

public void show(int x)throws Exception {

 

if(x>0){

 

throw new Exception();

 

} else {

 

System.out.println("show run");

 

         }

 

}

 

}

throws exception class declaration for, if this method may produce a variety of unusual circumstances, you can write multiple exception classes throws back, separated by commas.

A plurality of abnormality, for example :

public static int getElement(int[] arr,int index) throws NullPointerException, ArrayIndexOutOfBoundsException {

if(arr==null){

throw new NullPointerException ( "arr directed array does not exist");

}

if(index<0 || index>=arr.length){

throw new ArrayIndexOutOfBoundsException ( "standard error angle," + index + "does not exist in the index array");

}

int element = arr[index];

return element;

}

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/sunlangui/p/11525524.html