Getting Eclipse to trap on exceptions thrown only from my own code? - java

I'm using the most up-to-date version of Eclipse (Helios) for Java development. I've written a lot of code for my project, and I'm also using some 3rd-party code in the project.
It's normal for the 3rd-party code to internally throw exceptions, even when nothing is deeply wrong. It will catch these itself. During a normal run, the 3rd-party code might throw a lot of these not-really-a-problem Exceptions.
I'd like to tell Eclipse that, during debugging, it should break when any of my code throws an Exception, but not when other code I'm linking to throws an Exception. Does anyone know if Eclipse supports this?
I know Eclipse lets you break only when Exceptions of certain types are thrown, but that doesn't help when 3rd party code and my own both throw standard Exceptions.

AFAIK no. But you can set a root Exception and make all your exceptions extends it. Then you can set up a Exception Breakpoint on you root exception.
In the breakpoint window you can do so, there is an icon.

Related

JNativeHook doesn't work with Jython (JES)

I was trying to implement JNativeHook on jython, using JES.
I tried using JNativeHook on pure Java, and it does work as expected, yet, the same thing on jython throws an exception.
The exception NoClassDefFoundError occurs on GlobalScreen.registerNativeHook().
Why is that?
P.S. I checked the java.library.path of both Java and Jython, and they are the exact same.
Edit:
Tested some more and found out that the whole GlobalScreen class throws NoClassDefFoundError exceptions when using its methods.
Edit 2:
Doing dir(GlobalScreen) jython can actually see the methods of the class, yet exceptions get thrown on use.

Disable exception handling in Acceleo engine

In Acceleo, when I edit and save, say, generate.mtl, Acceleo automatically generates Generate.java class. From this java class I can call doGenerate method from an external class to generate my model-based stuff.
However, if there is some exception during execution, this exception is handled by the Acceleo engine. I would like to tell Acceleo engine not to handle exceptions, and thus realize that an error occurred.
How is it possible?
I've been thinking for a long time about this, without success. Last week, I successfully ran Acceleo in standalone mode, from a Java class instead of using the pluging.
It made me spend a lot of time with issues with libraries, problems with dependencies etc. but I finally got it (I mean, it's a hard work, be patient).
My surprise: When I ran Acceleo standalone it failed, but the templates were the same. I was having many errors, but the plugin was managing then and the plugin was printing an empty String as a result! Running Acceleo standalone, those errors raise an exception and my main class prints the stacktrace.
So, if you want to manage the errors by yourself, I recommend you to run it standalone but... good luck! :)
I hope this would help you :)

Being notified of caught exceptions in Eclipse / Java application

Is there a way to be notified of caught exceptions in an eclipse application?
I know that if I start an application using eclipse debugger, I can suspend execution upon caught and uncaught exceptions (see https://stackoverflow.com/a/3066280/228965). I guess this feature somehow uses JVMTI.
Now I have the following problem:
I have an eclipse application not written by me. I want to monitor this application. I have written some bundles to monitor different aspect of the application (user interactions, workbench changes, etc..). I start these bundles along the application using bundles.info file. Now I need to be notified whenever an exception happens. I added a listener to error log and this way I am notified of uncaught exceptions. However I want also to be able to be notified of "any" exception, even those that have been caught by the original developers.
Is there a way to achieve this?
You could investigate the logger of your application. If it use log4j, you could create an appender specific for exceptions and work with them.
Add a breakpoint to the constructors of java.lang.Exception (or maybe even Throwable, depending upon exactly what you're looking for).
All exceptions, even custom ones, must extend from one of these, so you can find each Exception as it is being created - and then even trace it through to see where it is being caught and handled (if at all).
Using AOP may also be a good option, but this approach doesn't require any modifications to the existing code - source code or byte code.
From JDK 1.5 you can use Thread.setDefaultUncaughtExceptionHandler() for exactly this purpose.

How to find all locations in a Java project where a RuntimeException is possible to be thrown?

I have a java project and I want a list of locations in source code where a RuntimeException might be thrown. Is there any tool or Eclipse plugin to do this?
With FindBugs or PMD the NullPointerExceptions might be located, but I need to locate others as well.
NullPointerException, ClassCastException and ArithmeticException are a special cases as they do not need to be explicitly thrown. However, everything else has to be thrown explicitly.
Searching for possible causes of NullPointerException is useful as its a common bug, however like all RuntimeException they are best avoided rather than handled. Searching for all possible causes is likely to give you a very large result which is unlikely to be useful. (I have been through this exercise myself and after lots of work didn't achieve much IMHO)
You are better off using code coverage and try edge case values to ensure you have good test cases which are likely to trigger all possible exception instead.
If you're only checking your own code, you can do this with Checkstyle. Look at the Coding Problems, Illegal Throws check.
You can specify any number of Exceptions, by default it is: java.lang.Throwable, java.lang.Error, java.lang.RuntimeException.
Note: This doesn't pick up subclasses of the above exceptions, it only picks up the classes themselves. It also does not check source code to which you don't have access, so runtime libraries won't be checked, for instance.
I do agree with the other answers. Since I don't know what you want to achieve, I want to offer a more direct answer to your question:
Unfortunately I know no tool that does this. Sorry. Depending on how important it is to you, you could invest some work yourself.
Check your code 'by hand'.
After that the JDK remains (and most likely some libs you are using). You can download the JDKs source code from Oracle. Than you can do a full text search for the occurrences of newly created exception instances again. Note, however, that even this tedious work will not give you all possible sources since some methods are missing there.

Getting a call hierarchy in java

I am having real trouble tracking down a bug and it would help be a lot to know which method called a certain method. Is there an easy way to get a call hierarchy from java? Java is a small part of the app so I cannot compile and run the whole app in eclipse/net beans so I don't have access to an IDE debugger's call hierarchy.
Thread.currentThread().getStackTrace();
or
Exception ex = new Exception();
ex.printStackTrace();
It's fairly slow, but fine for debugging purposes. API docs here.
Java is a small part of the app so I cannot compile and run the whole app in eclipse/net beans so I don't have access to an IDE debugger's call hierarchy.
You dont need to run the app at all. If you make a project in Eclipse, you can use its built-in Call Hierarchy tool to find all of the possible places that call a method.
There is a trade off: The Call Hierarchy tool will give you ALL of the places from where a method is called. This is a good if you want/need to know all the possibilities. Neslson's suggestion of Thread.currentThread().getStackTrace() will give you the places from where a method is invoked during the process of you program for this invocation. The nuance is that you might not run through every code path during this invocation. If you are seeing specific behavior and want to know how you got there, use the getStackTrace() option.
Have you tried using Java's remote debugging capability?
Google search to get you started if you haven't
The best thing to do is throw an exception, immediately catch it and analyze the stack trace.
I believe that is how Log4J is capable of logging the calling method.

Categories