First, Java exception is object-oriented. A Java's Exception is an object that describes exceptions. When an exception occurs, an Exception object is generated and placed in an exception's member function.
Java's exception handling is achieved by 5 keywords: try, catch, throw, throws and finally. 1. Abnormal processing: The error handling structure in the Java language consists of try, catch, finally three blocks. The TRY block storage will possibly an exception Java language, and manage the relevant exception pointer; Catch block followed by the TRY block to excite the captured exception; the Finally block contains the resource, handle, etc. of the clerver without release. Regardless of how the code in the TRY block exits, the FinalLy block will be executed. 2. Throw an exception: Java language may not be captured directly in the method, and use the Throw statement to throw an exception to the upper calorie. The throw statement is to clearly throw an exception; first you must get a throwable instance handle, pass the parameters to the Catch, or use the New operator to create one. For example: public void execute (string str, int index) throws whcaexception {try {// may generate exception code;} catch (exception e) {throw new WhcaException ("JB: M:" E.getMessage);}} Finally talk about the needs of using the "abnormal mechanism"
1. A number of different exceptions may be generated in a method, you can set multiple "exception" throwings to solve this problem.
2. "Exception" object After the generated point is generated, the full process of termination of life after being captured is actually a piping process, so you can use reasonable control to detect the granularity of "exception" as needed.
3. There is also a special case in the "abnormal mechanism" - RuntimeException "exception class", this "exception class" and all its subclasses have a feature, which is directly handled by Java virtual machine generation. Fall, that is, the virtual machine is captured by the virtual machine place in the method. Therefore, when throwing this "running abnormality" method is quoted, there is no need to have a try ... catch statement to handle "exception". 4. Capture an exception, it is necessary to properly process it. Calling PrintStackTrace helps the debugger, but there is no real handling exception!
The PrintStackTrace () method can only analyze the program operation, and the StackTraceElement class is provided in JDK1.4 to get the running of the program! example:
Import javax.swing. *;