Catch block does not catch exception, why?
I am coding a rogue-like console program but I have a question:
My code does not catch an exception. I have a void with for loops and
arrays. The error happens when the array is out of range. This happens
because the void doesn't check if the array is bellow zero or over the
maximum.
void lightUp(int pos)
{
//first and second
for (int i = 1; i > -2; i--)
{
int lolPos = pos + (i * columns);
for (int j = 0; j < 8; j++)
{
tiles[lolPos + j].isVisible = true;
tiles[lolPos - j].isVisible = true;
}
}
//third
for (int i = 2; i > -3; i -= 4)
{
int lolPos = pos + (i * columns);
for (int j = 0; j < 7; j++)
{
tiles[lolPos + j].isVisible = true;
tiles[lolPos - j].isVisible = true;
}
}
//fourth
for (int i = 3; i > -4; i -= 6)
{
int lolPos = pos + (i * columns);
for (int j = 0; j < 6; j++)
{
tiles[lolPos + j].isVisible = true;
tiles[lolPos - j].isVisible = true;
}
}
//fifth
for (int i = 4; i > -5; i -= 8)
{
int lolPos = pos + (i * columns);
for (int j = 0; j < 5; j++)
{
tiles[lolPos + j].isVisible = true;
tiles[lolPos - j].isVisible = true;
}
}
for (int i = 5; i > -6; i -= 10)
{
int lolPos = pos + (i * columns);
for (int j = 0; j < 3; j++)
{
tiles[lolPos + j].isVisible = true;
tiles[lolPos - j].isVisible = true;
}
}
}
So I made a catch block, in case the error happens my progam does not crash.
try
{
lightUp(player);
}
catch { }
But, I am still getting OutOfRange exceptions. They get not trapped by the
catch block. Why is that?
No comments:
Post a Comment