0

Well, im trying to calculate slope of a line, and naturally, when a point becomes undefined is when you try to divide by zero. Well I handle this in a try catch and but still getting a division by zero error, but in a rather... unexpected spot...

Here is the code:

private void tmrEnemyMovement_Tick(object sender, EventArgs e) {
        for (int i = 0; i < enemies.Count; i++) {
            int eneX = enemies[i].GetEnemyWorldPosX();
            int eneY = enemies[i].GetEnemyWorldPosY();
            int playerX = player.GetPlayerWorldPosX();
            int playerY = player.GetPlayerWorldPosY();

            double slope = -100000000;
            try {
                slope = (GetDistanceBetween(eneY, playerY)) / (GetDistanceBetween(eneX, playerX));
            } catch (DivideByZeroException) {//Division by Zero Exception is handled here.
                slope = GetDistanceBetween(eneY, playerY) / 1;
            }

            int multiplicative = 1;

            int rise = 1;//Convert.ToInt32(slope * multiplicative);
            int run = multiplicative;
            Text = Convert.ToSingle(slope) + "";

            if (enemies[i].GetEnemyWorldPosX() < player.GetPlayerWorldPosX() && enemies[i].GetEnemyWorldPosY() > player.GetPlayerWorldPosY()) {//Enemy Resides in Quadrant 2
                enemies[i].MoveEnemyTo(run, rise, "+-");
            } else if (enemies[i].GetEnemyWorldPosX() < player.GetPlayerWorldPosX() && enemies[i].GetEnemyWorldPosY() < player.GetPlayerWorldPosY()) {//Enemy Resides in Quadrant 3
                enemies[i].MoveEnemyTo(run, rise, "+-");
            } else if (enemies[i].GetEnemyWorldPosX() > player.GetPlayerWorldPosX() && enemies[i].GetEnemyWorldPosY() > player.GetPlayerWorldPosY()) {//Enemy Resides in Quadrant 1
                enemies[i].MoveEnemyTo(run, rise, "-+");
            } else if (enemies[i].GetEnemyWorldPosX() > player.GetPlayerWorldPosX() && enemies[i].GetEnemyWorldPosY() < player.GetPlayerWorldPosY()) {//Enemy Resides in Quadrant 4
                enemies[i].MoveEnemyTo(run, rise, "--");
            }
        }
        Bitmap bmp = map.GetMap() as Bitmap;

        using (Graphics drawEnemy = Graphics.FromImage(bmp)) {
            for (int i = 0; i < enemies.Count; i++) {
                drawEnemy.DrawImage(enemies[i].getSprite(), new PointF(enemies[i].GetEnemyWorldPosX(), enemies[i].GetEnemyWorldPosY()));
            }
        }
    }

And for some strange reason, I get this in a weird spot which doesnt really help me: picture

This method is defined as follows:

private int WorldPositionY = 0;

public int GetEnemyWorldPosY() {
    return WorldPositionY;
}

As you can see, its a simple getter, so why am I getting a division by zero error #1 at all, when it is handled in a try catch, and #2 on this line? Have I missed something?

**EDIT: ** Here is the stack trace: enter image description here

BlazeXenon
  • 40
  • 1
  • 8
  • Do you have optimizations disabled? – Rob Apr 22 '16 at 03:45
  • Currently, I am running this in debug mode. – BlazeXenon Apr 22 '16 at 03:46
  • Can you show the full stack trace? – Zong Apr 22 '16 at 03:47
  • Yes, one second, let me get that and update the post with it. – BlazeXenon Apr 22 '16 at 03:48
  • Okay, I have updated the post. – BlazeXenon Apr 22 '16 at 03:51
  • This is an aside, but it might be better practice to check the value of the denominator against 0 (and replace with 1 if so) rather than catching the exception and re-trying the calculation. – Rob Apr 22 '16 at 03:53
  • Oh yes, I know that is what I should do, but I was just at trying to see whether I could fix the exception being thrown by just using a try catch, but as you can see, for some reason that didnt work. – BlazeXenon Apr 22 '16 at 03:55
  • @BlazeXenon It's likely it is actually being handled (check the dialog box), but you've configured it to break on *all* exceptions. I am not sure why it's showing the incorrect line, though I've found that happens occasionally. You should be able to click okay and continue execution without any issue. Check out this question to change your configuration: http://stackoverflow.com/questions/4499473/how-to-tell-the-debugger-to-ignore-breaking-on-thrown-exceptions so that you only break on *unhandled* exceptions. – Rob Apr 22 '16 at 03:58
  • Yes, I forgot something important, you are right. I made it break on all exceptions because I wanted to see which line I might be getting an index out of bounds exception on, but via graphics, which instead of producing a exception, only produces a big red X. Due to this I set it to break on all exceptions and forgot to switch it back! Haha silly me, thank you! – BlazeXenon Apr 22 '16 at 04:05

1 Answers1

4

Try to recompile your program and ensure you are in Debug mode. Also do not use try/catch for calculating slope - this satement is very long. Use

var distanceX = GetDistanceBetween(eneX, playerX);
if (distanceX != 0)
    slope = GetDistanceBetween(eneY, playerY) / distanceX;
else
    slope = GetDistanceBetween(eneY, playerY);

And this exception you caught is not critical. You can press Continue and go on. Or you can switch off handled exceptions (uncheck user-handled in Debug->Exceptions->Common Language Runtime). Or you can run your program without attached debugger (use Ctrl+F5).

Artem
  • 1,535
  • 10
  • 13
  • Well, for some weird reason, my try catch didnt work, but the if else statement did.... kinda odd but never-the-less thank you for the help! – BlazeXenon Apr 22 '16 at 04:01