Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict lines of code in exception printStackTrace

try
{
 //logic
}
catch(Exception e)
{
      e.printStackTrace();
}

the printStackTrace() method will give us lines of statements related to exception but in which all lines are not useful for us

how we can restrict lines of exception details

like image 310
Mahender Ambala Avatar asked Oct 21 '25 03:10

Mahender Ambala


1 Answers

You can write the stacktrace into a PrintWriter, then take the output of the writer, and restrict the number of bytes, or lines, or filter it with a regular expression, or whatever fits your needs.

} catch (Exception e) {
    StringWriter sw = new StringWriter();
    e.printStackTrace(new PrintWriter(sw));
    String fullStackTrace = sw.toString();
    String restrictedStackTrace = ... //now you can manipulate the fullStackTrace
    System.err.print(restrictedStackTrace);
}
like image 73
Jan X Marek Avatar answered Oct 26 '25 18:10

Jan X Marek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!