Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

| Views: 428
Many a time when I'm debugging I feel quite lazy and often want to stop execution of code within a method before that method actually ends. The naive solution is to put in a return statement or throw an exception at the point where you want to stop running your code. Of course this doesn't work if you have lines of code after your return/throw clause.

The compiler will throw an error if you try this...
error: unreachable statement

...and a mandatory screenshot of the error...

Now you may be tempted to simply comment out the remaining lines, but depending on how much code you've got that could be more effort that it's worth.

So here's my solution to this. Put in an if statement that always evaluates to true and in the body of this statement either return from the method or throw an exception. This is a simple non-intrusive short circuit of the code, just remember to remove it when you're done with your debugging!

It looks something like this...
if (1 == 1) {


Now the compiler will not complain and your code will exit early as required.


Have comments or feedback on what I wrote? Please share them below! Found this useful? Consider sending me a small tip.
comments powered by Disqus
Other posts you may like...