With:
Object method(){
...
return /* some complex expression */
}
Is there a way I can see what value will be returned while debugging? Like somehow set a breakpoint that would be hit right after the return, but before execution goes to the calling code? I'd like to be able to know what the return value is and also be able to see what values of all of the local variables are.
The way I do it now is by making a temporary variable: Object ret = /* something complex */; return ret;
. But thats a hassle and also adds an extra unnecessary line of code.
Note: This is the same question as Can I find out the return value before returning while debugging in Visual Studio, but for intellij.
On IntelliJ IDEA 2016.3: it's hidden inside the cog button of the debug panel. Ensure Show Method Return Values
is checked.
https://i.stack.imgur.com/5ei7C.png
Use the debugger to break somewhere inside the function whose return value you'd like to see.
https://i.stack.imgur.com/8vLIg.png
Step out of the function (or step over until you escape):
https://i.stack.imgur.com/83Fvi.png
Observe that the return value appears in your variables:
https://i.stack.imgur.com/2qafi.png
There is Watch method return values button available in the debugger panel.
Watch method return values: Click this button to watch return values of the last executed method.
https://i.stack.imgur.com/wH2F5.png
IDEA 12 configuration:
https://i.stack.imgur.com/6Nm3c.png
There seems to be a couple ways you can do this. The first one involves setting up the breakpoint on the method signature, in your case you would setup a breakpoint on Object method(){ . This will allow you to watch for the entrance and exit of the method. I believe you have to use this in conjunction with "Watch method return values" like stated above, but I haven't been able to completely test this as it takes too long to compute. Beware, this does dramatically decrease the performance of the debugger and it will take longer to debug.
Also you can do the following manually.
Setup the breakpoint on the return line. When the return line is hit, click on the return line, specifically put the cursor on the operation that you want to see, then go to Run->Evaluate Expression (or Alt-F8) and it should run the expression at that point and return what it's returning.
Note: If you create a breakpoint, there are a lot of things you can tell IDEA to do with them, such as if you break on a breakpoint, you can tell them to perform an expression and log it. You can look around with what you can do if you right-click on a breakpoint and click properties.
UPDATE: Try this in conjunction with the first way of doing it. Don't use "Watch method return values" as it seems to slow down or freeze up the debugging session. Instead do the following
Right-click on the return expression you want to see and click "Add to Watches" Next add a method breakpoint like stated above. Debug your program and your method will break on the method signature. Hit the F9 key for continue and it should break again AFTER the return expression has been computed and your return expression should be in the watch column.
Remember that method breakpoints are slower so it might take more time, a tricky way to improve this if you are noticing too much of a performance hit is by just setting the return statement breakpoint (without having a method breakpoint) and then adding the method breakpoint AFTER the return statement breakpoint hits and then continuing.
Hope this helps,
This was asked a while ago, but I use a different method when I want to handle this situation.
When debugging, you can mark the expression (in your case, the expression right after the "return") and hit CTRL + ALT + F8 (Quick Evaluate Expression). IntelliJ will pop up a little window showing you the value that will be returned.
Success story sharing