Http://hi.baidu.com/geniusian/item/b06a1e51c6bc7c958c12edaa
Exception
1. in Java, an exception object is always an instance of the throwable subclass.
2. The error class system describes internal errors in the Java running system and resource depletion.
3. Errors Caused by programming may cause runtimeexception. Exceptions caused by other causes of errors-for example, errors that have run the correct program due to I/O errors do not cause runtimeexception.
4. Exceptions derived from runtimeexception include the following problems: 1> incorrect type conversion; 2> array out-of-bounds access; 3> attempting to access a null pointer.
5. Exceptions not derived from runtimeexception include: 1> trying to read data from the end of the file; 2> trying to open a URL in the wrong format; 3> try to use a string to construct a class object, and when the class corresponding to the string does not exist;
6. The principle for processing runtimeexception is: If runtimeexception occurs, it must be your error;
7. Different browsers can process different types of URLs. Therefore, whether the URL format is incorrect depends on the specific environment, not just the program code;
8. In Java language specifications, any subclass of error and the subclass of runtimeexception are called non-checked exceptions, while other exceptions are called checked exceptions;
9. An exception is thrown only in the following four cases: 1> a method that throws "Checked exceptions" is called, for example, the Readline method of the bufferreader class; 2> An error occurred while running the program, and throw a "Checked exception" with the throw statement; 3> program error, for example, if a [-1] = 0, an "unchecked exception" is thrown, for example, the array subscript is out of bounds (arrayindex -- outofboundsexception); 4> internal errors occur on the Java Virtual Machine or the Runtime Library;
10. Java internal errors do not need to be declared. A method must declare all "Checked exceptions" that it may throw ". If a method from the parent class is overwritten in its subclass. Note that if the parent class method does not throw any "Checked exceptions" at all, then the subclass can only do so (PS: If the subclass contains the parent class method, the "Checked exception" thrown by the subclass method cannot exceed the parent class method! Subclass must capture all "checked exceptions" and handle them by yourself !)
11. In Java, a method without a throws indicator cannot throw any checked exceptions. Therefore, the actionreceivmed method cannot throw any checked exceptions ";
12. For try/catch code blocks, if any code in the method throws an exception and its type is not specified in catch, the method exits immediately;
13. Capture and handle exceptions that are known to be handled (catch), and pass exceptions that do not know how to handle (throws );
14. For try/catch/finally code blocks, the code in the finally clause will be executed no matter whether exceptions are caught or not;
15. Although Java programmers must manually set code in the finally clause to recycle resources, Because Java has a "Garbage Collection" mechanism, only a small number of resources need to be manually recycled;
16. The actionreceivmed method cannot throw any checked exceptions (no throws indicator );
17. Tips for using the exception mechanism:
1> exception Control cannot replace a simple test. Compared with a simple test, it takes much more time to catch exceptions than the former. Therefore, exceptions are used only in the case of exceptions;
2> do not over-Detail exceptions;
3> do not suppress exceptions: the following format can be used: Try {// code} catch (exception e) {// null}
4> Do not be ashamed to pass exceptions: for some exceptions (such as exceptions generated by calling the fileinputstream constructor or the Readline method), it is better to pass exceptions than to capture them. It is more appropriate to allow high-level methods to notify users of errors or give up commands that fail to be executed;
18. Exception link: capture the original Exception A in the code, and a custom exception B corresponding to the error is thrown. If you need the original information, you only need to call B. getcause. The following is an example:
Public void actionreceivmed (actionevent event ){
Out = new filewriter (filename); // ioexception may be thrown.
}
Because the actionreceivmed method cannot throw any checked exceptions. If your program cannot handle this exception, you need to capture the exception and convert it to an unchecked exception. (Note: actionreceivmed cannot throw the checked exception. Therefore, you can throw it into an unchecked exception in the Code and let others handle it ).
Try {out = new filewriter (filename );}
Catch (ioexception checked) {runtimeexception unchecked = new runtimeexception (checked); throw unchecked}
The caller obtains the non-check exception after conversion. If you want to obtain the cause "cause", that is, the original check exception will be obtained through E. getcause.
19. Difference Between throw and throws:
A) The exception information that may be thrown by the throws declaration method (class) in a method (class) Declaration) throw is used internally to declare a specific exception information;
B) throws usually do not display caught exceptions. The system can automatically throw all captured exceptions to the upper-level methods. Throw requires you to capture related exceptions by yourself, then, related packaging is carried out, and the encapsulated exception information is finally thrown out;
C) The exception handling method is different. Throws does not handle the exception. Throws actively throw custom exception class objects who call and process the exception.
D) throws throw a class and throw an object.
E) throws can be used independently, that is, exceptions contained in throws can be captured by programmers rather than themselves. Throws cannot be used independently.ThrowIf an exception is not declared in the method declaration, the compiler reports an error;
Logs
18. You can call logger. getlogger ("Global"). info instead of system. Out. println to output debugging information (Info indicates the log record level );
19. The record name is similar to the package name and has a hierarchical structure. The recorder shares certain attributes with its subrecorder;
20. The system sets seven record levels for the recorder: Server, warning, info, config, fine, finer, and finest. by default, only three levels of records (info or above) are recorded. all to record all records, or use level. off to close all records;
21. log management is initialized during the VM startup process and is executed prior to the main method;
22. Like the log manager, the log processor also has a level (default info ). For a record to be recorded, its record level must be higher than that of the recorder and processor;
23. Log File mode variable: % H system variable user. Home variable
% T temporary system directory
% U is a unique number used to resolve name conflicts.
% G indicates the number generated for the log file to be rewound (% G suffix is used when the rewound function is used and the mode does not contain % G)
% Indicates % characters
24. to install a filter during the log recording period or in the processor, you only need to call the setfileter method, but we can only have one filter at most;
25. Call the setformatter method to install the formatter into the processor;
Assertions
26. By default, the city is closed. You can use-ea to enable it and use-da to disable it. Option-Ea:... enable the full-class assertion function in the default package, and enable or disable assertion as a class loader;
27. The-da and-ea options do not work for "System Classes" without classloaders. For these System Classes, use the-enablesystemassertions/-ESA option to enable the assertion function;
28. assertion tips: 1. assertion failure is a fatal and irrecoverable error; 2. assertion check is only used in the stage of program development and testing; therefore, the assertion should not be used as a signal to notify another part of the program of recoverable errors, or the assertion should be used as a way to notify the program user. Assertions should only be used in the test phase to locate internal program errors;
29. assertions are tools used in the testing and debugging phase, and logs are strategic tools used throughout the life cycle of the program.