Dividing by zero, on purpose

Started by
15 comments, last by Ezbez 10 years, 5 months ago

Just curious if anyone else does this.

Sometimes I have an if statement block I don't know is running. I know the code before, and after, the block is running fine.

So, I put a divide by zero after it. I know there are more sophisticated ways of debugging, and I have used them. But for a quick and dirty, I make an error by dividing by zero. I was going to stop the program anyways.

So I was just curious if anyone has ever purposely made an error in a section of code to see if part of it was running or not?

Advertisement

I usually just put in a breakpoint and watch to see if it's hit or not. If I have something more complex, I debug in the console and have each block print a message each time it's executed, which lets me home in on what I'm looking for pretty quickly. The latter is also nice for hunting down more specific issues inside of a block, rather than just seeing if it's executing or not.

I've never thrown in a line meant to give my compiler a conniption, but I guess that would work as well as anything else.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

I'm with Khaiy. Depending on the language, I either go with breakpoints, alerts/message boxes, or debug printing (usually to the console, but, for instance, PHP and webpages, I just print to the page).

The only time I've ever deliberately put a divide by zero into my code was to check if my exception handler was actually catching divide by zero and other non-C++ exceptions.

If I'm about to head home for the day and I'm working on a piece of code that is incomplete but compilable, I'll type some garbage like: "dgsahjgda". That way when I come back to it in the morning If I forget where I was up to, it'll take me straight to the garbage when I try to compile.

I'll do the same if I'm doing some manual refactoring involving a lot of copy and pasting.

[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler

If I'm about to head home for the day and I'm working on a piece of code that is incomplete but compilable, I'll type some garbage like: "dgsahjgda". That way when I come back to it in the morning If I forget where I was up to, it'll take me straight to the garbage when I try to compile.

I'll do the same if I'm doing some manual refactoring involving a lot of copy and pasting.

I do this too, but instead of garbage, I write down the possible reason for why the code might be failing, or whatever thought I happened to have about it.

I work mostly in C++, so I usually use __debugbreak() (or __asm int 3).

If I'm about to head home for the day and I'm working on a piece of code that is incomplete but compilable, I'll type some garbage like: "dgsahjgda". That way when I come back to it in the morning If I forget where I was up to, it'll take me straight to the garbage when I try to compile.

I'll do the same if I'm doing some manual refactoring involving a lot of copy and pasting.

I do this too, but instead of garbage, I write down the possible reason for why the code might be failing, or whatever thought I happened to have about it.

#error

is probably what you are looking for.

Breakpoints is what the OP is looking for?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley


Breakpoints is what the OP is looking for?

Yep.

#error is also good as a sanity check (is this bit of code even being compiled?!), which can occur if you have lots of conditional compilation going on (or inside a header file if you aren't sure whether it is being included).

EDIT: Dividing by zero isn't very reliable anyway, if you don't store the result it can be optimised out and floating point division by zero results in one of +/-infinity or NaN. If the compiler can detect an unconditional divide by zero it might give a compile time error too. So I second __debugbreak() or whatever does the same thing in your compiler.

EDIT2: Assuming C/C++ anyway

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement