How to specify a custom action on Java exception in Eclipse IDE? - java

I'm working in Java with Eclipse (Luna) and I would like to specify what action happens whenever my project throws an exception. In other words, anytime an error happens and Eclipse prints a stack trace to the console, I would like to run my own code to print the stack trace in my own debug window or save to a text file, for example.
I'm not sure if this is a problem for my java project (maybe by overriding printStackTrace()?) or for the IDE itself (through some setting, etc.) Either way it doesn't really matter since its just a debug feature and would be removed before I export anything public.
Thanks for your help
EDIT: To clarify, I'm not talking about exceptions only from a specific line/class. I'm familiar with try-catch blocks. What I want is to preform an action any time any exception or error happens anywhere in my code, even if there is a typo and my project wont compile.

NOTE: You cannot have your own debug window in IDE
If you want to log some of your exceptions to some file wherein latter on you can see, so its time you use log4j.
Start with this simple example.
You will find ample on google :)

Related

Debugging some code that is not printing any stack trace

So I am debugging someone else's customized code used in a Java based enterprise software. As per his documentation, a specific file has the customization I am looking into. I deployed all customized files on the test server and now the browser window crashes as soon as it starts to render data using that code. No stack trace gets printed out. The customized code in question is not actually causing the issue. The issues is caused by some other code that uses the data returned from this piece of code. The puzzle for me is that I don't really know where the data is being returned to, and any error being thrown is not printed out. Is there anything I can do to figure out what file uses the data returned by this customized code?
Brute force: Tell debugger to break on all exceptions.
E.g. in Eclipse, select menu "Run" > "Add Java Exception Breakpoint...", type Exception and select the java.lang.Exception, check both "Suspend on caught exceptions" and "Suspend on uncaught exceptions", and click "OK". In the "Breakpoints" view, select the new Exception breakpoint and check "Subclasses of this exception".
This is break on any thrown Exception (not Error). You may see a lot of false positives, which is why I call this the "brute force" method.
Change to Throwable if you want to catch Error too.

Eclipse debugging : Stack trace on event

Im trying to get the code flow of an open source platform. I have got the source code and ran the program from eclipse. The program has an option called "Run job" and I want to know where the control goes when that option is clicked. How can achieve this?
First, try to identify the control with the label "Run job".
You could do this by searching the source code in Eclipse with Search > File and then setting "Containing Text" to "Run job" and "File name patterns" to "*.java".
Probably in the same file, there is an ActionListener (or similar) added to the control that calls a method, when the control is clicked. This is the method you're looking for. (Add a breakpoint to see the flow in the debugger or try to understand it from the code.)
Apart from searching for the appropriate handlers and buttons in the source code (if you know the names), you can also enable tracing.
In your run configuration, there should be a tab for tracing. There, you'll want to enable some of the options under org.eclipse.ui that start with trace/.
You will get a lot of debug output, and there might be no trace option for the event you'd like to see. However it works well for things like keybindings (trace/keyBindings) and knowing which UI element got an event (trace/graphics). Note that some also take arguments, e.g. a commandId (something like org.eclipse.ui.edit.copy, will depend on your application).
You can find a small help text for each option here.

How on earth does he debug a running application like this, and more importantly, how can I?

"Debugception!"
You may notice that within the first 15 seconds of this YouTube video (from 1:01:01 to 1:01:16), Markus Persson (aka "Notch", creator of Minecraft) has somehow managed to save/update an application and attach a debugger to it while it was already under the process of being debugged, supposedly all with a simple keyboard shortcut. The previously coded application somehow magically became the newly edited one, and seemingly without relaunching it or spawning a new process... It's possible that this is just some form of locally remote debugging, but something about it just doesn't seem quite right.
I've spent several days Googling and asking around on how he was able to do this, yet to no avail. I've found no such option under Eclipse preferences, and whenever I try to save & debug an already running application, it simply launches a separate instance of the newly updated application, side-by-side with the older, outdated one.
Am I missing something? How was this possible?
How was he able to utilize such an astounding, powerful debugging feature?
Thanks in advance!
Update
Okay, so this appears to be a standard feature specific to Eclipse.
Coming from a background in NetBeans and Visual Studio, I'm astounded that this doesn't seem to exist elsewhere (or at least in NetBeans!)...
This is a built-in feature of Eclipse. If you edit a method while the program is running in debug mode, it will compile the new method, and replace the old method with the new version. If some thread was already running that method, it will jump back to the beginning (AFAIK; this might only happen when the program is paused).
You don't need to re-launch the program or set any special preferences. Just edit and save, and the magic will happen.
Eclipse can't always figure out how to merge your changes into the running program - usually if you changed anything outside a method body (including the method's parameters or return type). In this case, you will get a warning dialog, with the option to stop the program, restart the program or ignore the changes.

Ecplise View flow of running program

I'm currently writing a pretty large program that calls the same methods from different places.
Now I would really like to see how the program goes from one method to another as it is running. Like a live view that shows when what method is opened (and why?). Call Hierarchy doesn't suit my needs at this point. Is there a way?
One way to follow the logic of your application is by placing breakpoints at the line of code you want your application to stop at but, to do this you'll have to setup it up in debug mode.
Every major IDE will let you do this, including Eclipse.
Have a look at this tutorial:
Java Debugging with Eclipse
Once you setup your program in debug mode you can add a breakpoint in the gutter next to the line numbers.

eclipse debug stop on error line

In Visual studio when an error happens during debug, the line causing the error is jumped to in the source code and the error displayed.
Is there a way with Elcipse to do the same? The Debug perspective appears, but the line causing the error and the error message are not immediately apparent.
I'm assuming you are working with Java, so the answer is yes. You can tell Eclipse to break on exception. From the Run menu, select Add Java Exception Breakpoint.... Now, you need to select the Exception class. If you wish to stop on all Exceptions, you can use java.lang.Throwable. You can also specify whether to stop on caught or uncaught exceptions.
You can (and should) also use it for assertions, as I outlined in this post.

Categories

Resources