tags:

views:

38

answers:

2

Is there a way to view stack elements like un-assigned return values or exceptions that not assigned to a local variable? (e.g. throw new ...)

For example, suppose I have code along the lines of:

public String foo(InputStream in) throws IOException {    
    NastyObj obj = null;
    try {
        obj = new NastyObj(in);
        return (obj.read());
    } finally {
        if (obj != null) obj.close();
    }
}

Is there any way to view the return or exception value without stepping to a higher level frame where it is assigned? This is particularly relevant with exceptions because you often have to step back up through a number of frames to find an actual handler.

I usually use the Eclipse debugging environment, but any answer is appreciated. Also, if this cannot be done, can you explain why? (JVM, JPDA limitation?)

A: 

Couldn't you just catch the IOException (and name it) and then rethrow it? (Don't know Java, but that's what I'd do in C++ and Python.) Of course, this answer is invalid if you can't edit the given code or are inspecting the state right now and need to know what it looks like... But if you can edit the code, that seems like the way to go.

dash-tom-bang
Thanks, but no, I don't want to edit the code. Usually when this is an issue I'm debugging third-party code and don't want to step deep into (or recompile) NastyObj because it's nasty.
gibbss
+1  A: 

The answer seems to be that both JPDA/JDI and Eclipse are deficient. I’ve verified the following, but I’m not going to bother posting the code unless someone really needs it.

For the most part, JDI mirrors the structure of the code. Named locals for a given scope can be obtained through the debuggee thread’s current StackFrame. Unscoped locals and method arguments can be obtained through the debuggee thread’s current Method. However, in general where the documentation refers to a LocalVariable, it is a named local.

It IS possible to get the return value of a function, if the function is returning normally and you are using a 1.6 debugging setup (RFE). If the debugger listens for MethodExitEvent, then event.returnValue() gets the value returned after all the method has finished executing. According to the documentation, this event is NOT generated when a method terminates via a thrown exception.

The conclusion is that if you use JDI directly under Java 1.6, you can get the return value of a function before it's assigned to a local, if it returns normally. The Eclipse and Netbeans UIs don't support this. You can't get information about Exceptions that have been thrown but not caught yet.

gibbss