1. Multi-use Try,catch; do not have a try,catch containing all content
Benefits: Different modules grab different exceptions, a certain module is hanging, do not affect the program of other modules
2. Write a few more catche; try not to use exception this big exception to accommodate all the anomalies
Not in order to pursue the simplicity of the code, Try,catch only write one, use exception to crawl all possible anomalies, this is only the ideal state, the program error is not directly printed out the exception is done, should be in the Catche grab the exception at the same time to the programmer output error log, On the one hand to do some processing feedback to the user, such as some hint error box or error page, can not let users do not know why but the system is not normal.
To sum up, you should use a few catche blocks to crawl different exceptions, and then do different processing, return to the user, as far as possible different exceptions using different Cacht block crawl A exception crawl all the anomalies;
3. Pass the test to talk about the difference of Java common abnormal printing
Packagecom.hudong.test;
Importorg.apache.commons.logging.Log;
Importorg.apache.commons.logging.LogFactory;
public class trycatchtest{
Private final static Log logger =logfactory.getlog (trycatchtest.class);
public static void Main (string[] args) {
try {
System.out.println (1/0);
catch (Exception e) {
Logger.error ("---fill---:" +e.fillinstacktrace ());
Logger.error ("----------Beautiful dividing line---------");
Logger.error ("---msg---:" + e.getmessage ());
Logger.error ("----------Beautiful dividing line---------");
Logger.error ("---E---:" +e.tostring ());
Logger.error ("---e===e---:" + e);
System.out.println ("===============");
System.out.println (E.tostring ());
System.out.println ("--------------------");
System.out.println (E.getmessage ());
System.out.println ("--------------------");
E.printstacktrace ();
System.out.println ("--------------------");
SYSTEM.OUT.PRINTLN ("---fill---:" +e.fillinstacktrace ());
System.out.println ("===============");
}
try {
System.out.println (1/0);
catch (Exception e) {
try {
Throw e;
Throw E.fillinstacktrace ();
catch (Throwable E1) {
E1.printstacktrace ();
}
}
}
}
About the Java exception print on the web has a lot of data on the log4j log of the abnormal output also has a lot of information; But today suddenly came up with a question: the difference between several types of abnormal printing, and which one can print out detailed exception trajectories and specify error lines; No exact statement or information was found online. , so I wrote a test program, through the data to confirm the following conclusions:
1. There are only two scenarios in the test case where detailed error stack information can be printed:
Logger.error ("---e===e---:" + e);
Throw e;
The first is the LOG4J definition of the error (Message,e) method implementation of the print details; the second is the way Java brings in detailed exceptions; only two of them can print out detailed stack tracks, except that the error message is output in other ways.
2. The difference between throw E and Throwe.fillinstacktrace ()
The former outputs a detailed error code line, which outputs only the position where the exception was thrown, and does not print out the location of the error code line, that is, the E.fillinstacktrace () Location code line; So, relatively speaking, you should use the former if you want to clearly throw an error message.
the difference between 3.logger.error ("---e===e---:" + E) and logger.error ("---fill---:" +e.fillinstacktrace ());
Throw, Throwe.fillinstacktrace () Two can print a detailed exception, and, unlike the log4j, print out a detailed exception and point to the error code line, which does not print the detailed stack.
4.fillInStackTrace Output Information View
If you call E.fillinstacktrace () to repopulate the information in the stack, you lose information about the offending object in the lower S-level environment. If you re new, a violation also loses information about the lower level of the offending object.