C# try/catch best practices for optimal performance
I've heard that try/catches are considered to be bad for performance, but
that if you're rarely expecting to throw exceptions then they are
considered a better way to return failure information rather than using
methods/functions with Boolean values.
Does using try/catch in this manner better or worse for performance? It
certainly makes coding easier.
Example:
void DoSomething(){
try{
DoSomethingIffy();
} catch {
// Yikes! Do failure stuff
}
}
void DoSomethingIffy(){
if (rareCondition) {
throw new Exception("oops");
}
}
vs
void DoSomething(){
if (!DoSomethingIffy()) {
// Yikes! Do failure stuff
}
}
bool DoSomethingIffy(){
if (rareCondition) {
return false;
}
else {
return true;
}
}
No comments:
Post a Comment