-------Android Training, Java training, look forward to communicating with you! ----------
Exceptions are anomalies that occur when the program is compiled and run. The exception is encapsulated by object-oriented programming and described in the form of a Java class.
I. Abnormal system
1. throwable
Error
• There are often major problems such as the absence of a running class or a memory overflow.
• Do not write against code for its handling
exception
• Everything that happens at run time needs to be handled or thrown inside the program.
For the convenience of memory and writing, the subclass of error and exception has the suffix name of its parent class name.
Common methods in the 2.Throwable class
GetMessage ()
Gets the exception information that returns the string.
ToString ()
Gets the exception class name and exception information, which returns a string.
Printstacktrace ()
Gets the exception class name and exception information, and the bit that the exception appears in the program
Reset return value void.
Printstacktrace (PrintStream s)
This method is typically used to save the exception content in a log file to check
Read.
Two. How to handle exceptions
Java provides specific statements to handle exceptions, mainly in two ways.
1.try Catch Finally statement processing
Format:
Try
{
Code that needs to be detected;
}
catch (Exception class variable)
{
Code to handle exception (processing mode)
}
Finally
{
Statements that are bound to be executed;
}
2. There may be a problem with the function of declaring this feature through the throws keyword
The following code
classmodel{ Public Static voidMain (String[]args) {Try{div (5,0);//The statement that the exception may occur}Catch(Exception e) {System. out. println ("The divisor cannot be 0.");//The processing e.printstacktrace after the exception capture (); }finally{System. out. println (" Over"), or the statement that will be executed regardless of whether an exception occurred, except in the case where the program ended earlier, such as System.exit (); Exit program}} Public Static intDivintAintb) throws exception{//here to declare an internal possible exception to be thrown, handled by the callerreturnA/b; }}
Three. Custom exceptions
The exception object is often encapsulated with a custom exception when it is necessary to process some exceptions that occur in the context of the program's running environment.
The custom exception class needs to inherit from exception or its subclasses. The exception information is defined by the constructor, and the exception is thrown by the throw keyword.
The following code
classFushuexception extends exception{Private intvalue; Fushuexception ()//Because the constructor is overloaded, you need to manually write the parameterless constructor {super (); } fushuexception (String msg,intvalue) {Super (MSG);//Use the parent class's constructor to handle the exception information This. Value =value; } Public intGetValue () {returnvalue; }}classdemo{intDivintAintb) throws fushuexception//inside throws an exception, must be externally identified, RuntimeException exception and its subclass exception is not required to be identified. { if(b<0) Throw NewFushuexception ("There is a case where the divisor is negative------/by Fushu", b);//When an exception occurs by manually declaring the exception to be thrownreturnA/b; }}classmodel{ Public Static voidMain (string[] args) {Demo d=NewDemo (); Try { intx = D.Div (4,-9); System. out. println ("x="+x); } Catch(fushuexception e) {System. out. println (E.tostring ()); System. out. println ("the negative number of the error is:"+E.getvalue ()); } finally{System. out. println (" Over"); } }}
Four. Exception extensions
1. Handling of multiple exceptions
When declaring an exception, it is recommended to declare a more specific exception. This can be handled more concretely.
The other side declares several exceptions, corresponding to a few catch blocks. Do not define an extra catch block.
If an inheritance relationship occurs in multiple catch blocks, the parent exception catch block is placed at the bottom.
2.RuntimeException
Run-time exceptions, RuntimeException, and their subclasses, if thrown in a function, do not have to be identified on the function
3. Common exception classes
Arithmetic Exception class: Arithmeticexecption
Null pointer exception class: NullPointerException
Type cast Exception: ClassCastException
Array negative subscript exception: Negativearrayexception
Array subscript out-of-bounds exception: ArrayIndexOutOfBoundsException
File ended exception: Eofexception
File not found exception: FileNotFoundException
string conversion to numeric exception: NumberFormatException
Input/Output exception: IOException
Black Horse Programmer _java Exception overview