Java exception handling principle and LOG4J output detailed exception analysis

Source: Internet
Author: User
Tags getmessage log4j

1. Multi-use Try,catch; do not have a try,catch contain all content

Benefits: Different modules grab different exceptions, a module abnormal hanging, do not affect the other modules of the program

2. Write a few more catche; try not to use exception as a big exception to accommodate all exceptions.

Do not want to pursue the concise code, Try,catch only write one, use exception to crawl all possible exceptions, this is just the ideal state, the program error is not directly print out the exception is finished, should be in the Catche crawl exception on the one hand to the programmer output error log, On the one hand to do some processing feedback to the user, such as some error box or error page, can not let the user does not know why but the system is not normal.

In summary, should use more than a few catche block to crawl different exceptions, and then do different processing, return to the user, as far as possible different exceptions use different Cacht block grab a exception grab all the exceptions;

3. Pass the test to talk about the differences in Java Common exception 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 ();

}

}

}

}

There is a lot of information on the Java Anomaly Print network, and there is a lot of information about the abnormal output of the log4j log. But today, there is a problem: the difference between several abnormal printing, and which can print out the detailed anomaly trajectory and specify the error line; No exact statement or information was found on the Internet. , so I wrote a test small program, by checking the data to confirm the following conclusions:

1. In the above case, there are only two cases where you can print detailed error stack information:

Logger.error ("---e===e---:" + e);

Throw e;

The first is the print details that are implemented by the log4j defined error (Message,e) method, and the second is the way in which Java comes with a detailed exception, which is tested only to print detailed stack tracks, and the other way is to only output error messages.

2. The difference between throw E and Throwe.fillinstacktrace ()

The former outputs a detailed error code line, the latter only outputs the location where the exception was thrown, does not print out the location of the error code line, that is, the e.fillinstacktrace () local line of code, so relatively, if you want to clearly throw the error message should use the former.

3.logger.error ("---e===e---:" + E) differs from logger.error ("---fill---:" +e.fillinstacktrace ());

Throw, Throwe.fillinstacktrace () Two can print out detailed exceptions, unlike log4j, which prints detailed exceptions and points to the line of error code, which does not print a detailed stack.

4.fillInStackTrace Output Information View

If you call E.fillinstacktrace () to repopulate the information inside the stack, you will lose information about the offending objects in the low S-level environment. If you re-new a violation also loses information about the lower level of the offending object.

Java exception handling principle and LOG4J output detailed exception analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.