Java Stacktrace Not Printing Exception

I have the following code:

import org.apache.commons.lang.exception.ExceptionUtils; public void myMethod() { try { // do something } catch (Exception e) { System.out.println(ExceptionUtils.getStackTrace(e)); // prints "java.lang.NullPointerException" System.out.println(ExceptionUtils.getFullStackTrace(e)); // prints "java.lang.NullPointerException" e.printStackTrace(); // prints "java.lang.NullPointerException" } } 

The output I would like to see is a complete stack table with line numbers and a class hierarchy that did not execute. For instance,

 Exception in thread "main" java.lang.NullPointerException at org.Test.myMethod(Test.java:674) at org.TestRunner.anotherMethod(TestRunner.java:505) at java.util.ArrayList(ArrayList.java:405) 

This code runs inside a larger application that also has log4j, but I hope to get an exception in the string, so I can send it as an email to java developers.

Does anyone have any ideas on how I can capture a full stack trace into a string? I can not use Thread.currentThread (). GetStackTrace () since this application runs in Java 4. What can block the above code from printing the full stacktrace?

+5
source share
8 answers

An exception can be caught and thrown somewhere in front of the catch block. Perhaps in another class that you call to make logic.

An Exception created as
new Exception(new Throwable("java.lang.NullPointerException"));
prints something like what you see.

+3
source

this is beacause java does some code optimization when starting in server mode (java-servererver) skip this. use -XX:-OmitStackTraceInFastThrow in java args see link below for details:

http://jawspeak.com/2010/05/26/hotspot-caused-exceptions-to-lose-their-stack-traces-in-production-and-the-fix/

+15
source

If you re-throw an exception, the JVM stops filling the stack trace. I'm not sure why, but it could be to reduce the load on the JVM. You need to look at the previous stack trace to see the details.

 for (int n = 0; ; n++) { try { Integer i = null; i.hashCode(); } catch (Exception e) { if (e.getStackTrace().length == 0) { System.out.println("No more stack trace after " + n + " thrown."); break; } } 

prints

 No more stack trace after 20707 thrown. 
+11
source

Does anyone have any ideas on how I can capture a full stack trace into a string?

You can use the method below (StringWriter.toString ()) to wrap the stack trace in a string.

  StringWriter writer = new StringWriter(); e.printStackTrace( new PrintWriter(writer,true )); System. out.println("exeption stack is :\n"+writer.toString()); 
+2
source

Maybe you do not have a console output application. You can add it. If not, register it as LOGGER.error (ex); with log4j or SLF4J

+1
source

I can only think of 2 reasons why e.printStackTrace() can only output the string "java.lang.NullPointerException" .

  • Something might cause setStackTrace(new StackTraceElement[0]) to throw an exception.
  • The exception object can be an instance of a complex class that overrides printStackTrace() or some other method to return misleading information.
+1
source

Make sure you use e.printStackTrace () instead of e.getStackTrace ();

0
source

What can block the above code from printing a full stack trace?

In my case, the Exception superclass that I caught had the following code:

 /* avoid the expensive and useless stack trace for API exceptions */ @Override public Throwable fillInStackTrace() { return this; } 

This meant that the stack trace array was never populated, so all I got was:

 org.apache.kafka.AuthException: Auth failed: Invalid username or password 

without information about the stack, so I have no idea where this happens. Beautiful.

I was able to override this behavior by taking the source for Exception , copying it to my project and removing the fillInStackTrace() method. This is a hack, but I needed information from this Exception .

0
source

Source: https://habr.com/ru/post/1433598/


All Articles