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

NOTE: This article is 3 years or older so its information may no longer be relevant. Read on at your own discretion! Comments for this article have automatically been locked, refer to the FAQ for more details.
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.


Skip down to comments...
A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. If you spot something out of place, please do let me know.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
comments powered by Disqus
Other posts you may like...