1. Exceptions
try{
Code that may appear to be abnormal
}
catch (Exception ex) {
}
finally{
Freeing resources
}
2. Exceptions for advanced applications
Opening and closing principle: Closed to modification, open to new
3. What is an exception?
Parsing: An exception is an error that occurs when the code is run
int result=5/0;
*********************************************
Exceptions: 01.5 keywords Try Catch finally throw throws
02.try catch Three application scenarios
Try no exception execution code after catch
An exception in try jumps into catch execution and then executes subsequent code
Try with exception, catch catch, exception type mismatch, result program interrupted
03.try Catch finally three combinations of illegal forms
In one case, finally does not execute, catch has System.exit (0): Meaning
Java Virtual Machine exit
04. Face Questions
A return condition in a catch
try{
return "0";
}
catch () {
return "1";
}
finally{
System.out.println ("Execution");
}
Conclusion: Although there is a return in the code, the finally block will still be executed
05.Throwable: Divided into two types of exceptions: one is a run-time exception and one is to check for an exception
06.throw is commonly used by programmers to manually throw their own exception information
throw new Exception ("error hint");
07.throws: Declaring exceptions
public void Test () throws exception{
}
08. Exception Inheritance Diagram
4. How do I log logs using log4j?
Parsing: Introducing JAR Packages
Create a Properties file
Writing log4j Configuration Code
log4j Logging
5. Can be enumerated, can imagine the situation of abnormal information, as far as possible to avoid abnormal capture, because
It consumes a lot of program performance.
6. Official documentation
http://dlc-cdn.sun.com/jdk/jdk-api-localizations/jdk-api-zh-cn/builds/latest/html/zh_CN/api/
7. If you choose Java, it means that you have selected the configuration file (4k)
System.Configuration
8. Getting the object of a class in general
Method One: The lowest level is new ()
SqlDataReader Dr=cmd. ExecuteReader ();
Method Two: Factory object
Method Three: Configuration file
Conclusion:
1, regardless of the occurrence of wood anomalies, finally block code will be executed;
2, when there is return in the try and catch, finally will still execute;
3, finally is executed after the return of the expression after the operation (at this time does not return the value after the operation,
Instead, save the value that you want to return, regardless of the code in the Finally, the returned value will not change, it is still the previously saved value.
So the function return value is determined before the finally execution;
4, finally, it is best not to include return, or the program will exit early, the return value is not a try or catch in the saved return value.
Example:
Case 1:try{} catch () {}finally{} return;
Obviously the program executes sequentially.
Case 2:try{return;} catch () {} finally{};
The program executes code in the try block before return (including expression operations in the return statement);
Then execute the finally block and finally execute the return in try;
The statement after the finally block return, because the program has already been return in the try and is no longer executed.
Case 3:try{} catch () {return;} finally{} return;
The program executes the try first, and if an exception is encountered to execute a catch block,
There is an exception: executes the code in the catch before return, including the expression operation in the return statement, and then executes all the code in the finally statement,
Finally executes the return in the catch block. Finally, the code at 4 is no longer executed.
No exception: After executing the try and finally return.
Case 4:try{return;} catch () {} Finally{return;}
The program executes code in the try block before return (including expression operations in the return statement);
Then execute the finally block, because there is a return in the finally block and exit early.
Case 5:try{} catch () {return;} Finally{return;}
The program executes the code in the Catch block before return (including the expression operation in the return statement);
Then execute the finally block, because there is a return in the finally block and exit early.
Case 6:try{return;} catch () {return;} Finally{return;}
The program executes code in the try block before return (including expression operations in the return statement);
Exception: Executes the code in the Catch block before return, including the expression operation in the return statement;
Then the finally block is executed because there is a return in the finally block and exits early.
No exception: Then the finally block is executed, because there is a return in the finally block and exits early.
Final conclusion: Any execution of a return statement in a try or catch will be preceded by a finally statement, if finally exists.
If there is a return statement in Finally, then the program will return, so the return in the finally is bound to return,
The compiler implements the return in finally as a warning.
Here's a test program.
public class Finallytest { static void main (string[] args) { System.out.println ( new Finallytest (). Test ());} static int Test () { int x = 1 try {x ++; return X;} finally { ++X;}}
The result is 2.
Analysis:
In a try statement, when the return statement is executed, the result to be returned is ready, at which point the program goes to finally executes.
Before you go, try to store the result to be returned in a different local variable from the X, and after the finally is executed, the returned result is removed from it.
Therefore, even if the variable x is changed in Finally, it does not affect the return result.
It should use the stack to hold the return value.
Exceptions in Java