What is an exception:
An exception is an error that occurs while the Java program is running.
Sao dialect:
The world's most true dependence is you in the try I catch, no matter what you temper, I quietly accept, silently deal with (this can not remember)
Exception Inheritance system diagram:
How to handle Exceptions:
try{
Used to detect anomalies.
} catch () {
That is used to catch exceptions.
}finally{
Statements that are bound to be executed.
}
Throws: Declares an exception that the method may want to throw.
Throw: Throws an exception manually.
exception Handling (Java provides unique statements to handle exceptions) :
1 Public classDemo1 {2 Public intDemo1 (intAintbthrowsexception{3 //There may be a problem with the functionality of declaring this feature through the throws keyword. 4 //The caller must throw this exception (throws Exception) in the main function or catch the exception (try Catch)5 returnA/b;6 }7 Public Static voidMain (string[] args) {8Demo1 de =NewDemo1 ();9 Try {Ten intDemo1 = de. Demo1 (1, 0); One System.out.println (demo1); A}Catch(Exception e) {//Exception e = new ArithmeticException (); -SYSTEM.OUT.PRINTLN ("Divisor cannot be zero"); -System.out.println (E.getmessage ());//Get exception information theSystem.out.println (E.tostring ());//call the ToString method to print the exception type and exception information -E.printstacktrace ();//The JVM handles exceptions this way by default -}finally{ -System.out.println ("Ten years drink ice, difficult cold blood");//the code in finally will certainly execute + } - } +}
Custom Exceptions:
unique exceptions appear in the project, which are not described by Java and encapsulated into the object.
These peculiar anomalies can be thought of as Java encapsulates the problem. will be peculiar to the problem. Make a custom package
When a throw throws an exception object inside the function, the corresponding processing action is required. (1.try catch 2. The function declares that it is handled by the caller).
Exceptions are divided into two types of exceptions:
Compile-time exception: When compiling a program, it is possible that something like this happens, such as the file can not be found, such an exception must be compiled at the time of processing, if not processed, but compiled.
Run-time exception: The programmer has made a mistake, need to come back to modify the code
Interview questions:
The difference between 1.throws and throw:
Throws:throw:
* Used after the method declaration, followed by the exception class name * used in the method body, followed by the exception object name
* Multiple exception class names can be separated by commas * Only one exception object name is thrown
* Indicates that an exception is thrown and handled by the caller of the method * means that an exception is thrown, handled by the statement in the method body
the difference between 2.final,finally and finalize:
*final: Can be decorated class-the class being decorated cannot be inherited by a decorated method-cannot be overridden by a modifier variable-can only be assigned once
*finally: is a statement body in a try statement and cannot be used alone to release resources
* Finalize:finalize () is a method that is called by the object's garbage collector when a garbage collection period determines that no more references to objects exist
3. If there is a return statement in the catch, will the finally code be executed? If so, is it before or after return?
1 Public classDemo1_demo1 {2 Public Static voidMain (string[] args) {3Demo DM =NewDemo ();4 intDr =Dm.getdemo ();5 System.out.println (DR);6 }7 }8 classdemo{9 Public intGetdemo () {Ten intX =10; One Try { ASystem.out.println (1/0); -X=20; - returnx; the}Catch(Exception e) { -X=30; - returnX//first create a tunnel, after the execution of filally release resources, the tunnel will not change, so the value is -}finally{//do not write the return statement in Finally, because the function of finally is to release resources, +x=40;//will be executed, and if you write a return statement inside, then the result of the try and catch will change, so this is the ~ - } + } A}
Exception considerations:
* A: When a subclass overrides a parent class method, the child class's method must throw the same exception or subclass of the parent exception. (father is broken, son can not be worse than father)
* B: If the parent class throws more than one exception, when the subclass overrides the parent class, it can only throw the same exception or a subset of his, the subclass cannot throw exceptions that the parent class does not have
* C: If the overridden method does not throw an exception, then the method of the subclass must not throw an exception, if there is an exception in the subclass method, then the subclass can only try, not throws
Java exception Hyper-detailed summary