0

I want to make a generalized Exception handling in my program. It should show an JOptionPane.showMessageDialog(null, "There was an unexpected Exception: "); liek this. Now I also want to add the necessary information to the dialog. I know how to get the type of the exception (e.getClass();), but is there a way to only get the information like which class/method and which line?

Example for the exception: at com.mainfirst.bloomberg.invoice.report.Etst.main(Etst.java:35)

I would like to only get the part inside the brackets. i tried Thread.currentThread().getStackTrace()[2].getLineNumber() what i found in a similar question, but this returned null, so i tried with other indexes and index 1 returns the line the Thread.currentThread()... is written in. Is there way to retrieve the information i need?

Community
  • 1
  • 1
Aelop
  • 156
  • 9
  • Why aren't you taking a look on an other answer of the similar question? – Paul Wasilewski Dec 07 '16 at 12:07
  • i did and yet i don't get the right information – Aelop Dec 07 '16 at 12:10
  • Possible duplicate of [How can we print line numbers to the log in java](http://stackoverflow.com/questions/115008/how-can-we-print-line-numbers-to-the-log-in-java) – Paul Wasilewski Dec 07 '16 at 18:44
  • @PaulWasilewski if you've read the question properly, you would have seen, that i posted the link to that question and stated, that it did **not** work as they posted – Aelop Dec 08 '16 at 07:12
  • How about the answer how Juan? http://stackoverflow.com/a/5916374/1405363 Looks to me pretty equal to the accepted answer? `StackTraceElement l = new Exception().getStackTrace()[0]; System.out.println( l.getClassName()+"/"+l.getMethodName()+":"+l.getLineNumber());` – Paul Wasilewski Dec 08 '16 at 11:09

2 Answers2

0
Pattern p = Pattern.compile("/\([\w\W]*\)/");
Matcher m = p.matcher(e.printStackTrace());

JOptionPane.showMessageDialog(null, "There was an unexpected Exception: "+m.group(1))

EDIT:

use e.getStackTrace() instead of e.printStackTrace(), so you can get the exception message as a string.

use m.group(0) instead of m.group(1) in order to match the whole regex. In this case, it would have the same effect, but if you had more groups in the regex, you would have been able to select which group to match.

This specific regex match only the first occurrence, as there is no /g modifier at the end of it.

Igino Boffa
  • 411
  • 2
  • 6
  • 26
  • i am not very used with matcher.group method. does this only return the first matched result? also this code doesn't work because `e.printStackTrace()`is a void method and not a string – Aelop Dec 07 '16 at 12:16
  • Although this might solve the problem, one should always add an explanation. – BDL Dec 07 '16 at 13:52
0

but this returned null, so i tried with other indexes and index 1 returns the line the Thread.currentThread()... is written in

It's because you are using Thread.currentThread().getStackTrace() not e.getStackTrace(). Get the line number like so:

try{
   method();
}
catch(Exception e){
   e.getStackTrace()[0].getLineNumber();
   e.getStackTrace()[0].getFileName();
   e.getStackTrace()[0].getMethodName();
}

It will print line number exception occurred, next indices pointing to outer methods in chain.

Dominik G
  • 594
  • 6
  • 8