Do not rely on exceptions in your code and write code that avoids exceptions. Since exceptions cause performance to suffer significantly, you should never use them as a way to control normal program flow. If it is possible to detect in code a condition that would cause an exception, do so. Do not catch the exception itself before you handle that condition. Do not use exceptions to control logic. A database connection that fails to open is an exception but a user who mistypes his password is simply a condition that needs to be handled. Common scenarios include checking for null, assigning a value to a String that will be parsed into a numeric value, or checking for specific values before applying math operations.
//Unnecessary use of exception
try
{
value = 100/number;
}
catch(Exception ex)
value = 0;
Agree on "don't use exceptions to control program flow", but as to the "exceptions cause performance to suffer significantly", it's not quite as simple as that. See here for an interesting article on the subject: http://www.programmersheaven.com/user/pheaven/blog/175-Do-trycatch-blocks-hurt-runtime-performance/
Jax 2/27/2008 9:19:25 AM
So the conclusion from the artcile is that .NET is optimized for the case where you do not get exceptions. Therefore "exceptions cause performance to suffer significantly" :) Good article. Very interesting details.
kostya.ly 3/9/2008 1:23:32 AM