1. Definition of exceptions
An exception is an abnormal event that occurs during runtime , which causes the program instruction process to execute abnormally.
2. Classification of exceptions
Error: An internal error or resource Exhaustion of the JVM system is a burden to the JVM .
Exception (Exception): A general problem caused by programming errors or other external factors.
Note: Programmers only need to handle exceptions (Exception) and cannot handle errors.
650) this.width=650; "src=" Http://images0.cnblogs.com/blog2015/639084/201507/071644019084526.png "style=" border:0 px; "/>
Error: Generated and thrown by the Java Virtual Machine , the program does not handle it
Exception (Exception): The parent class of all exceptions, typically requiring the user to display a declaration or capture.
Runtimexception: A class of special exceptions, such as by 0, the null pointer array out of bounds, and so on, it produces more frequent, processing trouble,
If the displayed claims or captures will have a significant impact on program readability and operational efficiency, the system automatically detects and hands them over to the default exception handlers.
2.1 Exception Classification (from a programmatic point of view)
Non-inspected (unchecked) exception: The compiler does not require a forced disposition exception .
Usually refers to the logic error when programming, the thing that the programmer should avoid
Java.lang. RuntimeException and its subclasses are non-inspected exceptions
Wrong type conversions: java.lang.ClassCastException
Array subscript out of bounds: java.lang.ArrayIndexOutOfBoundsException
Null pointer access: java.lang.NullPointerException
Arithmetic exception (except 0 overflow): java.lang.ArithmeticException
Checked exception: The compiler requires an exception that must be disposed of . Refers to the general anomalies caused by external factors when the program is running.
The class with the specified name was not found: java.lang.ClassNotFoundException
Access to files that do not exist: Java.io.FileNotFoundException
Exception occurred while manipulating the file: java.io.IOException
Exception occurred while manipulating the database: Java.sql.SQLException
650) this.width=650; "src=" Http://images0.cnblogs.com/blog2015/639084/201507/071659321585863.png "style=" border:0 px; "/>
3. Mechanism of exception handling
If an exception occurs during the execution of the Java program, an exception class object is automatically generated and automatically submitted to the JVM, a process known as throw (throw) exception
After the JVM receives the exception object, it looks for the code that can handle the exception and hands the exception object to it for processing, which is called catching the exception and handling the exception
If the JVM cannot find the code that catches the exception, the runtime system terminates and the corresponding Java program exits.
4.try--catch--finally
try{}: An area of code blocks that may occur where an exception occurs, producing one or more types of exception objects that will be processed by the subsequent catch statement
catch (): You can have multiple catch statements that catch multiple types of exceptions, but the exception subclass must precede the exception parent class.
Finally (): the uniform export of exception handling, which is bound to execute the code block, but it is optional, typically handles resource files such as closing open files, deleting temporary files, and closing database connections.
5.throws Keywords (method)
Used when defining a method, indicating that the exception produced by the method is not processed, and is given to the method at the call of the processing
public void method throws e1,e2{};
6.throw keywords (in-method)
Throws an exception in the method (the throw new Exception ();), if the exception is a checked exception, it must be handled as follows, if it is a non-checked exception is OK
Handle this thrown (examined) exception, you can choose to try-catch it , or use throws to throw it out of the way.
7. Custom Exception Usage
Public class exceptiontest { public static void main (string args[]) throws MyException { int y; int x=y=0; int z=x/y; if (y==0) throw new myexception ("denominator is 0"); } }class MyException extends Exception { public MyException () { super (); } puBlic myexception (string msg) { super (msg); } }
Summary:
exception-throwing principle:
1, only one is executed with the exception-corresponding catch module, and the remaining catch modules are not executed. And
Java only catch the first discovered exception, no longer executes, that is, the following
The code that may have an exception is no longer executing and catch it.
2, the catch module with the parent class is placed on the last side, otherwise it cannot compile the past.
The embodiment of the exception in the child parent class coverage;
1, when the subclass overrides the parent class, if the method of the parent class throws an exception, then the child class's overriding method can only throw the exception of the parent class or the subclass of the exception.
2, if the parent method throws more than one exception, the child class can only throw a subset of the parent exception when overwriting the method.
3, if no exception is thrown in the method of the parent class or interface, then the subclass cannot throw an exception when overriding the method.
If the subclass method has an exception. You have to do a try processing. Absolutely not to throw.
finally{} code block:
The finally zone executes as long as the catch or the preceding statement uses the return statement, unless System.exit (0) is executed between them, and the finally region does not execute
throw and throws differences:
Throw is used inside the method: throw new Exception (); Throws is used on the method definition: public void method Throws Exception
Note: If the thrown exception is a non-checked exception (the RuntimeException subclass), you do not need to catch/throw the exception, unless the exception is being inspected.
java-Abnormal article "three"