Sometimes I get the feeling that I'm abusing of the try/catch formula. Maybe I'm just paranoid about good coding practices, but I was wondering, would you consider a bad idea to use try catch to avoid borderline crashing situation?
Let me explain: let's say you have something that changes overtime, and you know that it could happen that it goes to a division by zero or to an array out of range exception.
Quite often, it happens that the only thing that you would do in those case would simply be closing the method with a return;
. This would require trying to understand in which situations the exception would occur, and then return in those cases.
In many cases this could be a painful task, and this is when I simply catch what happens and return.
if (/* Potentially multiple peinful search for crashing points*/)
return;
I do
try
{
//I know it's going to crash here sometimes
}
catch { return; }
Sometimes this is very heandy, but it feels like kinda cheating.
To make an example: I was just coding a thing with two TrackBars interacting with each other, and in certain circumstances if one of the two is brought to Min or Max it would create a division by zero. I am just happy with the UI staying at the very ValueChanged
event before the crash, so this solution would be just fine.
What do you think about this? Is it considered some kind of horrible coding thing, or people does it?