edit: Someone had added the C# keyword. I am NOT talking about C#, just exceptions in general. Specifically, exceptions in compiled languages like C++ and D; though C# was also in my mind.
I asked a question about exceptions and I am getting VERY annoyed at people saying throwing is slow. I asked in the past How exceptions work behind the scenes and I know in the normal code path there isn't any extra instructions (read the accepted answer) but I am not entirely convinced throwing is more expensive then checking return values. Consider the following
{
int ret = func();
if (ret == 1)
return;
if (ret == 2)
return;
doSomething();
}
vs
{
try{
func();
doSomething();
}
catch (SpecificException1 e)
{
}
catch (SpecificException2 e)
{
}
}
As far as I know there isn't a difference except the if
s are moved out of the normal code path into an exception path and an extra jump or two to get to the exception code path. An extra jump or two doesn't sound like much when it reduces a few if
s in your main and more often run) code path. So are exceptions actually slow? Or is this a myth or an old issue with old compilers?