"Java" control exception in console output

Source: Internet
Author: User
Tags throwable

Every time in the Java process problems, always throw a lot of anomalies, people dazzling, in the Javaweb programming, especially, it is difficult to find the location of the error,

Especially in your network engineering files, plug-ins, frameworks, a variety of plug-ins, frame chain, resulting in you more difficult to find the wrong location.

In fact, the exception in the console output can be controlled.

The key is to use this method with arrays and objects that are Java-free, without introducing anything:

Stacktraceelement[] Stacks = new Throwable (). Getstacktrace ();
This operation.

Look at a program first,

public static void Exceptionmethodunhandle () {int a = 1;int b = 0; System.out.println (A/b);}
This procedure is obviously in addition to 0 exceptions, the following error will occur when running directly:


We'll first find that the main function in the exception method in line 41st is an error in zooming in, and then we'll go back and look at the 35th line exception the Exceptionmethodunhandle in this class.

If there is a A/b in line 35th, this problem is 0.

However, in some large project programming, will spit out a bunch of anomalies, you can hardly step by step backward.

Change the program to this:

public static void Exceptionmethod () {try {int a = 1;int b = 0; System.out.println (A/b);} catch (Exception e) {System.out.println (Gettraceinfo ());}}
Requires Java to pass the Gettraceinfo () method output error:

public static String Gettraceinfo () {StringBuffer stringbuffer = new StringBuffer (); Stacktraceelement[] Stacks = new Throwable (). Getstacktrace (); Stringbuffer.append ("Exception in [Class:"). Append ( Stacks[1].getclassname ()). Append ("][method:"). Append (Stacks[1].getmethodname ()). Append ("][line:"). Append ( Stacks[1].getlinenumber () + "]"); return stringbuffer.tostring ();}
The output is then changed to this:


Direct indication of an exception in the Exceptionmethod method of the exception class of this program.

The reason we can output this is to focus on the Gettraceinfo () stacktraceelement[of this method] stacks = new Throwable (). Getstacktrace (); method.

This array is as follows:


As you can see, the No. 0 element of this array is itself, and then the 1th element, which is the method, class, and number of rows that causes it to appear, and the 2nd element records the method, class, and number of rows that caused the first element to fail, that is, the root cause of the error is the first element of this data.

So in some cases, we can just output this element. Do not have to be long the abnormal brush screen.

If the program is normal, according to Java's try and catch, it is not possible to trigger the Catch method at all. For example, the following whole program, execute Normalmethod () then normal output 1, execute Exceptionmethod () output exception, and finally execute Exceptionmethodunhandle () will output the entire exception by default:

public class Exception {public static String Gettraceinfo () {StringBuffer stringbuffer = new StringBuffer (); Stacktraceelement[] Stacks = new Throwable (). Getstacktrace (); Stringbuffer.append ("Exception in [Class:"). Append ( Stacks[1].getclassname ()). Append ("][method:"). Append (Stacks[1].getmethodname ()). Append ("][line:"). Append ( Stacks[1].getlinenumber () + "]"); return stringbuffer.tostring ();} public static void Normalmethod () {try {int a = 1;int B = 1; System.out.println (A/b);} catch (Exception e) {System.out.println (Gettraceinfo ());}} public static void Exceptionmethod () {try {int a = 1;int b = 0; System.out.println (A/b);} catch (Exception e) {System.out.println (Gettraceinfo ());}} public static void Exceptionmethodunhandle () {int a = 1;int b = 0; System.out.println (A/b);} public static void Main (String args[]) {Normalmethod (); Exceptionmethod (); Exceptionmethodunhandle ();}}
The results of the operation are as follows:


"Java" control exception in console output

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.