Is there a way to tell the debugger to stop just before returning, on whichever statement exits from the method, be it return, exception, or fall out the bottom? I am inspired by the fact that the Java editor shows me all the places that my method can exit - it highlights them when you click on the return type of the method declaration, (Mark Occurrences enabled).
[eclipse 3.4]
Put a breakpoint on the line of the method signature. That is where you write
public void myMethod() {
Then right-click on the breakpoint and select "Breakpoint Properties". At the bottom of the pop-up there are two checkboxes: "Method Entry", "Method Exit". Check the latter.
You can set a method breakpoint.
Double click in the margin next to the method declaration. A breakpoint with an arrow decoration appears. Right-clicking to examine the properties, you can set "Suspend on:" for "Method Entry" and/or "Method Exit".
You can read more about them in the Eclipse Cookbook.
Good question. Off the top of my head, I'd do this:
public void method(Object stuff) {
try {
/* normal code */
} finally {
int x = 0;
}
}
You can set the breakpoint on the x = 0 line, and it will ALWAYS be executed no matter where you return. Even with an exception being thrown, it will be run.
The catch to this is scope. Unless you define variables outside of the try block, you won't be able to see their values where you get to the finally block since they will have exited scope.
Having to just place 5 breakpoints (one for each return statement, whatever) may work best.
I hope there is a better way, I'd love to know it.
Related
How to avoid java warning which says
The value of the local variable is not used
for the variable which is declared as private?
You have multiple choices:
Remove the field.
It is unused, so it shouldn't be there.
Comment out the field, e.g. using // TODO
Good for temporary hiding of warning until you write code using field.
Suppress the warning using #SuppressWarnings("unused").
Disable the warning in IDE settings. For Eclipse, that would be in
Window > Preferences
Java > Compiler > Error/Warnings
Unnecessary code > Unused private member
Select option Ignore
For #3 and #4, though, although you can, why would you want to?
Since it is not used and does not contain any code you are interested in, you can delete it.
It is saying because the value of the variable or variable is not used within the program. So to remove this warning you can simply delete this variable because you are not using it anywhere .Or use this variable at some point in your code
Actually, in some cases it's not that easy. Here's an example of why it's not as easy as just commenting out the code. Sure this is a comment, but I don't have rep of 50 to comment and the comment block is lame anyway. So sue me. This does not work by the way, but comments are lame here.
try {
// so, rather than a wait, we check for exit value
// and if that tosses an exception, the process is
// still running. Ooooooookkkkkaaaaaayyyyyy No Problem
int exitValue = pShowProcess.exitValue();
// guess we don't do this to get rid of the not referened warning
//(void)exitValue;
// we don't care what the exit value was
exitValue = 0;
// but if we get here, then the show stopped, so
// if we stop it now, it won't need to wait, it will be fine
// we think.
endShow();
} catch (Exception ex) {
// Process is still running. So just keep going until
// mouse clicks or something else stops the show
}
I found out ways to terminate (shut-down or stop) my Java programs. I found two solutions for it.
using return;When I want to quit or terminate my program execution , I add this.
using System.exit() ; Sometimes I used it. I read about System.exit() from this question.
So, I know a little on both them. But I am still confused as to how they actually work. Please check below codes...
public class Testing {
public static void main(String... str) {
System.out.println(1);
System.exit(0);
System.out.println(2);
return;
}
}
I am sure that 2 will not appear. I would like to know is why return; or other codes can write below the statement of System.exit(0); and what was real definition for return; (because it is strange thing for me return without any variables or values) ?
Calling System.exit(0) (or any other value for that matter) causes the Java virtual machine to exit, terminating the current process. The parameter you pass will be the return value that the java process will return to the operating system. You can make this call from anywhere in your program - and the result will always be the same - JVM terminates. As this is simply calling a static method in System class, the compiler does not know what it will do - and hence does not complain about unreachable code.
return statement simply aborts execution of the current method. It literally means return the control to the calling method. If the method is declared as void (as in your example), then you do not need to specify a value, as you'd need to return void. If the method is declared to return a particular type, then you must specify the value to return - and this value must be of the specified type.
return would cause the program to exit only if it's inside the main method of the main class being execute. If you try to put code after it, the compiler will complain about unreachable code, for example:
public static void main(String... str) {
System.out.println(1);
return;
System.out.println(2);
System.exit(0);
}
will not compile with most compiler - producing unreachable code error pointing to the second System.out.println call.
System.exit() is a method that causes JVM to exit.
return just returns the control to calling function.
return 8 will return control and value 8 to calling method.
Because System.exit() is just another method to the compiler. It doesn't read ahead and figure out that the whole program will quit at that point (the JVM quits). Your OS or shell can read the integer that is passed back in the System.exit() method. It is standard for 0 to mean "program quit and everything went OK" and any other value to notify an error occurred. It is up to the developer to document these return values for any users.
return on the other hand is a reserved key word that the compiler knows well.
return returns a value and ends the current function's run moving back up the stack to the function that invoked it (if any). In your code above it returns void as you have not supplied anything to return.
System.exit() terminates the JVM.
Nothing after System.exit() is executed.
Return is generally used for exiting a method. If the return type is void, then you could use return;
But I don't think is a good practice to do it in the main method.
You don't have to do anything for terminate a program, unless infinite loop or some strange other execution flows.
So return; does not really terminate your java program, it only terminates your java function (void). Because in your case the function is the main function of your application, return; will also terminate the application. But the return; in your example is useless, because the function will terminate directly after this return anyway...
The System.exit() will completly terminate your program and it will close any open window.
Well, first System.exit(0) is used to terminate the program and having statements below it is not correct, although the compiler does not throw any errors.
a plain return; is used in a method of void return type to return the control of execution to its parent method.
What does return; mean?
return; really means it returns nothing void. That's it.
why return; or other codes can write below the statement of System.exit(0);
It is allowed since compiler doesn't know calling System.exit(0) will terminate the JVM. The compiler will just give a warning - unnecessary return statement
So I noticed while debugging a Netbeans Java application when a function call is used in an assert function, you cannot hit a breakpoint within that function or step into that function.
At first I thought it had to do something to do with using an overridden function and my overridden function not being called, but I confirmed that is not what is going on. It still gets called, but cannot be stepped into.
Here is the snippet that I tried:
public class Example
{
public static boolean blah()
{
System.out.println("Executing"); //**Breakpoint here
return true;
}
public static void main(String[] args)
{
assert(blah()); //Cannot step into or hit breakpoint on this line.
blah(); //Can here.
}
}
Anybody have any ideas why this is not working?
By default, assertions are disabled at runtime.
Perhaps your debugging JVM does not have assertions enabled.
To enable assertions, specify the -enableassertions (or -ea) switch for your NetBeans debugging JVM parameters. Even though assertions are compiled into the bytecode, they won't execute without this switch.
See this link for more
Maybe, just maybe, every assert statement is optimized out in this build? Are you sure your print statement is executed twice?
The whole point of assert is that you can tell the compiler to just ignore what's inside when you want to optimize your code.
if (true) {
String a = "foo";
String b = "bar";
}
If I set a breakpoint at String a = "foo"; eclipse will stop, and I can step over and see the value of a inside the variables window. But I can't step over the 2nd statement, it just leaves the code block and I never see the value of b.
This forces me to add a noop statement after String b = "bar"; just so I can see what b contains. I also can't add a breakpoint on the closing } which I think are maybe related issues.
I know Visual Studio allows this, so is there a way to do this in Eclipse?
To set a breakpoint at the end of an arbitrary block is not possible (without byte-code hacking).
If it is a method body, then it is possible: you can set a Method breakpoint. Do this by double-clicking on the method definition line:
(notice the little arrow?) and then in the breakpoints view, select the breakpoint to see both an Entry and an Exit option tick-box in the displayed properties:
The little arrow indicates that, by default, we have set a breakpoint on entry to the method.
Now select Exit (and deselect Entry) and you will see this in the breakpoints view:
(There is a different little arrow, indicating an exit breakpoint.)
Now run the debugger on this little breaker ('Debug As a Java Application') and it will stop on the exit brace of the method:
and the local variables (only a in this case) are now visible (with the correct values) in the Variables view:
It is worth noticing that this type of breakpoint traps method exit however this happens -- even, for example, if we exit by throwing an exception.
You can highlight the expression on the right hand side of the assignment and press Ctrl+Shift+I (for 'inspect' I think). It will evaluate the expression and give you the result. That way you can know the value of b without needing a breakpoint after the assignment.
I'm honestly not sure what's going on in your Eclipse installation. I'm working in Java and just tried everything I can think of. I checked a static method for breakpoint on the last line that is not a } and it works just fine. I checked the same for a non-static method.
It breaks down this way: you can't breakpoint on a curly brace end, but it will default to whatever is under the curly brace, line-wise. So if you have a nested function:
public int DoesStuff(int x, int y) {
if(x > y) {
DoThing;
}
else {
DoOtherThing;
}
}
Then the last two braces cannot be break-pointed, but DoThing and DoOtherThing can. Make sense?
If ending statement is assignment and it is on local variable why do you want to see its value because it will not be in scope anymore and can't effect.
If it's setting same class attribute then you can see it when you return from this call and you have the object on which this method operated.
Although this doesn't answer the question but I am trying to understand the use-case of your problem.
I am creating breakpoints to debug my java application , and am using netbeans 6.8 (if this is relevant).
Basically the problem I have is that when I set a breakpoint and debug the program will break before it executes the code at that line. So for example if I have the following.
public static void someMethod() {
Object iWantToInspectThis = someOtherMethod();
if(somethingThatIsFalseInThisCase == true)
{
doSomethingElse();
}
}
So , I want to set a watch on iWantToInspectThis object and then break after it is set to the return value of someOtherMethod() so I can see what it is set to.
But if I set a breakpoint on that line then I will not see the result of this line being executed, and I cannot set the breakpoint later (inside the if statement) because then it will not be reached.
The only way I can manage to do this is to add a useless line like System.out.println("this is pointless"); after the call and break there instead which seems like a stupid way to have to do it.
How does everybody else deal with this?
Put a break on that line, and use step into next line (F8 if i remember correctly) so the line executes, and then you can see the value by hovering the mouse into the variable, or using a watch.