Exceptions: Important points of knowledge
The code of exception handling is simple, important or understanding its thought
I. Overview:
An
exception is an abnormal condition that occurs at run time
Throwable:error typically have major problems such as running a class that does not exist or a memory overflow that is not written against the code for its processing exception at run time, can be obtained through the TRT catch finally
The subclass names for exception and error are suffixed with the name of the parent class
Exception Example: Array out of bounds, pass parameter error ah ...
Class Futime{}class Bigtime{}public class Main{public static void Main (string[] args) {/*int[] a = new int[5]; * a = Nu ll System.out.println (a[5]);//Abnormal */sleep (-5);} /*public static void sleep (int t) {if (t<0) {///Data error, handling//Data error, handling//Data error, handling//..... Poor reading correction See}else if (t>10000) {//Data error, handling//Data error, handling/Data error, processing method new Bigtime ();} else {System.out.print ("Rest" +t+ "sub-species");}} */public static void sleep (int t)//This is a lot clearer {if (t<0)//But notice there is a problem, who is calling this function, how to receive an exception {//throw new Futime in the way thrown () ;//Representative time is negative, this object contains the name of the question, information, location and other information}else if (t>10000)//For example: I caught a cold { throw new Bigtime ();//c is taking medicine, getting on the hospital ..., And Java can understand how to find a cure for colds}else {System.out.print ("rest" +t+ "divide");}}}
*
* This shows the difference between Java and C, C is to use if judgment and then write a lot of solutions, and Java is used
* The form of the class describes and encapsulates the object in an abnormal situation
* A class that describes an unhealthy condition, called an exception class
* Previous normal process code and problem-handling code combined
* Now separate the normal process code from the problem-handling code to improve reading
The exception is that Java encapsulates the problem as an object through object-oriented thinking
* Describe it with an exception class
*
* Summing up a sentence is a different problem with different classes for specific descriptions, such as the corner of the cross-border, null pointer
*
two. Anomaly System
If a lot of the problem means that the class described before a lot of
to extract their generality, the formation of the exception system
The final problem (abnormal) is divided into two categories
throwable://Whether it is error, or abnormal problem, The problem should be thrown, let the caller know and handle the parabolic
(there are two big pies under the parent class)
1. Generally not handled. Use the error class to represent
2. can be handled. Use exception
system features:
Throwable and all its subclasses are parabolic, not this system.
How to embody the parabolic?
via two keywords: throws and throw, all classes and objects manipulated by these two keywords have a parabolic
(error and exception similar to disease, curable, and incurable)
Error:
Feature: Is the problem of the severity thrown by the JVM, has affected the operation of the program
the occurrence of this problem is generally not targeted processing, because cannot handle, directly modify the program (int[] A = new int[1024*1024*800], open a 800M array)
Exception A lot of direct subclasses, the direct sub-class of error is not many, but are not processed, more ruthless
system features :
The suffix of the subclass is suffixed with its parent class name, read strong, such as OutOfMemoryError
Three, the principle and the throw of the exception object
Small Demo:
Class Exceptiondemo{public int method (int[] Arr,int index) {if (arr==null) throw new NullPointerException ("How can an array reference be empty?"). if (index>=arr.length) {//return-1; Why not write return, because it is not op throw new arrayindexoutofboundsexception ("Array corner label out of Bounds" + index);//throw new ArrayIndexOutOfBoundsException ("" +index);//default}if (index<0) {throw new ArrayIndexOutOfBoundsException ("The array angle label cannot be negative" +index);} return arr[index];}} public class Main{public static void Main (string[] args) {int[] a = new int[10]; Exceptiondemo C = new Exceptiondemo (), int num = C.method (a,20);//int num = C.method (null,2); System.out.print ("num" +num);}}
Iv. Custom exception classes and their throws
Similar to a negative angle label exception this is not defined in Java, it is possible to follow the creation of Java exceptions, the negative angle of the exception of the case of a custom description, and encapsulation of the object (custom exception)
PS: If a class is called an exception class, then this class must inherit the exception system, only the subclass of the exception system can be parabolic, can be throw,throws two keyword operations
Class Fuexception extends Exception{fuexception () {}}//After defining an exception, you need to tell the caller what the problem might be class Exceptiondemo {//either capture or throw public int method (int[] Arr,int index) throws Fuexception{if (index<0) {throw new Fuexception ();} return arr[index];}} public class Main{public static void Main (string[] args) throws fuexception{int[] A = new int[10]; Exceptiondemo C = new Exceptiondemo (); int num = C.method (a,-2); System.out.print ("num" +num);}}
These two throws correspond to the
v. Differences in compile-time detection exceptions and runtime exceptions
Classification of exceptions:
1. Compile-time is detected exception: As long as the exception and its subclasses are, in addition to the special sub-class RuntimeException system
PS: Once this problem occurs, you want to test at compile time, so that the problem has a corresponding processing way, such problems can be targeted to deal with
2. No exception is detected at compile time (runtime exception): It is the compiler that runtimeexception and its subclasses do not detect this system at all
PS: This problem does not allow the function to continue, the operation can not be carried out, more because of the caller's cause or caused by the internal state changes caused by the
Then this problem is generally not handled, directly compiled through, at run time, let the caller call the program to force stop, let the caller modify the code
(Development of frequently encountered anomalies)
So when customizing exceptions, either define exception, or RuntimeException
the difference between throw and throws
1.throws used on functions
Throw is used within a function
2.throws throws an exception class, can throw multiple, separated by commas
Throw throws an object
Java Learning Lesson 18th (Exceptions and ideas (i))