Java debugging tip

Here's a little trick to help you debug your Java programs. When an Exception occurs and it's not caught somewhere, you get a stacktrace to help you locate the problem. When you do catch an Exception you can still call printStackTrace() on it. But what if you just like to see the call stack? Just make an Exception yourself!

(new Exception()).printStackTrace();

Well actually the method is defined in Throwable, so you might as well write

(new Throwable()).printStackTrace();

I find this very usefull when using the Observable pattern, as in the Java Swing API (there it's called listeners). One message can start a cascade of other messages throughout the system. This way you can see where it comes from. Use sparingly!

[tags]Java, debug, exception, observable, listener, pattern, swing[/tags]

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Syndicate content