Exception
Exception: is the abnormal condition that occurs when the program is running.
The origin of the anomaly: The problem is also a specific thing in real life, can also be described in the form of Java classes, and encapsulated into objects. In fact, Java is a description of the abnormal situation after the object.
The problem is divided into two kinds: one is a serious problem, the other is a non-serious problem.
- For serious, Java is described by the error class. For error It is generally not written with specific code to handle it
- For non-critical, Java is described by the exception class. For exception can be handled with a targeted approach
Whether error or exception have some common content, such as: abnormal information, cause and so on.
Throwable
|-----Error
------XXXX
|-----Exception
------XXXX
Handling of exceptions
Java provides a unique statement to handle
Format:
Try { catch(Exception class variable) { finally { The statement that must be executed;}
Common method operations on caught exception objects are:
- String getMessage (); Gets the exception information.
- String toString (); Get exception Name: Exception information
- void Printstacktrace (); Gets the exception name, exception information, the location of the exception, in fact, the JVM default exception handling mechanism, is called printstacktrace (), print the exception on the stack trace information
The sample code is as follows:
classDemo {intDivintAintb) {returnA/b;//new ArithmeticException (); }} Public classExceptiondemo { Public Static voidMain (string[] args) {Demo d=NewDemo (); Try { intx = D.Div (4, 0);//new ArithmeticException ();System.out.println ("x=" +x); } Catch(Exception e) {//Exception e = new ArithmeticException ();SYSTEM.OUT.PRINTLN ("except 0"); System.out.println (E.getmessage ()); ///By ZeroSystem.out.println (e);//Exception Name: Exception informationE.printstacktrace ();//Exception name, exception information, location where the exception occurred//in fact, the JVM default exception handling mechanism is to call Printstacktrace (), print the exception on the stack of trace information} System.out.println ("Over"); }}
Declare the exception (throws) on the function. Facilitates security, allows callers to process, and does not handle compilation failures.
As follows:
classDemo {intDivintAintbthrowsException {//The feature may be problematic by declaring the function with the throws keyword returnA/b;//new ArithmeticException (); }} Public classExceptiondemo { Public Static voidMain (string[] args) {Demo d=NewDemo (); Try { intx = D.Div (4, 0);//new ArithmeticException ();System.out.println ("x=" +x); } Catch(Exception e) {//e.printstacktrace ();System.out.println (e.tostring ()); } System.out.println ("Over"); }}
Handling of multiple exceptions
- When declaring an exception, it is recommended to declare a more specific exception so that the processing can be more specific
- The other side declares several exceptions, which correspond to several catch blocks, and do not define redundant catch blocks. If an inheritance relationship occurs in multiple catch blocks, the parent exception catch block is placed in the last
It is recommended that you define the specific handling in catch when you are doing catch processing. Don't simply define a sentence: E.printstacktrace (), or simply write an output statement.
Examples are as follows:
classDemo {intDivintAintbthrowsArithmeticException, ArrayIndexOutOfBoundsException {//The feature may be problematic by declaring the function with the throws keyword int[] arr =New int[A]; System.out.println (arr[4]); returnA/b;//new ArithmeticException (); }} Public classExceptiondemo { Public Static voidMain (string[] args) {Demo d=NewDemo (); Try { intx = D.Div (5, 0);//new ArithmeticException ();System.out.println ("x=" +x); } Catch(ArithmeticException e) {//e.printstacktrace ();System.out.println (e.tostring ()); System.out.println ("Except zero."); } Catch(arrayindexoutofboundsexception e) {//e.printstacktrace ();System.out.println (e.tostring ()); System.out.println ("The horn is out of bounds."); } Catch(Exception e) {System.out.println ("Haha:" +e.tostring ()); } System.out.println ("Over"); }}
Object-oriented exceptions