In the .NET programming sphere, code that feels like it might generate exceptions is put in a try{} block which would sound an alarm whenever one of these occur during execution. This "thrown" exception is caught by a catch {} block and handled appropriately.
Even though this feature makes our programming lives easier it has a drawback being a certain reduction in performance. This means that the number of exceptions thrown is inversely proportional to your application's performance. There are other operations in .NET that have similar effects, like boxing and unboxing of value types.
I ran a little test to check this out. I intentionally produced an exception and handled it while measuring the time it took to do this.
int[] Array = null; //not instantiated
Stopwatch watch = new Stopwatch();
try
{
watch.Start();
Array[0] = 1;//exception thrown
}
catch (NullReferenceException)
{
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds +"ms");
}
watch.Reset();
Console.ReadLine();
I got 23ms as an average of three executions. To serve as comparison I used a foreach construct to iterate a numeric array.
int[] numArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Stopwatch watch = new Stopwatch();
watch.Start();
foreach (int number in numArray)
{
Console.WriteLine(number);
}
watch.Stop();
Console.WriteLine();
Console.WriteLine(watch.ElapsedMilliseconds + "ms");
Console.ReadLine();
I got 4ms as an average of three executions.
With these results I would say that the process of throwing and handling exceptions is quite slow.
But that alone does not justify avoiding them. Exceptions should be thrown when needed. Your code would be better off having just the right quantity of exception throwing and handling structures than none, which could bite you pretty hard at runtime.
Exception-safe code is your best bet to minimizing the number of exceptions thrown instead of throwing them at the slightest provocation. Take a look at this code:
public string[] Getnames()
{
string[] array = null;
if (/* new names are available */)
{
//populate the array with names
return array;
}
return array;
}
public void PrintNames(string[] nameArray)
{
foreach (string name in nameArray)
{
//print the name
}
}
The PrintNames method could unnecessarily throw an ArgumentNull exception if there are no names to populate the string array.
The problem between these two methods is the state of the return type of GetNames() .If there are no names when GetNames() is called then the returned array would be null.
string[] array = null;
.............
return array;
In order to avoid an ArgumentNull exception when calling PrintNames() a defualt array could be returned when there aren’t any new names to populate the array with.The reifned code would look like this:
public string[] Getnames()
{
string[] array = null;
if (/* names are available */)
{
//populate the array with names
return array;
}
return new string[0];
}
Using default states over null(the almighty) saves you some exception hassle ,the trade-off though is memory resource.Little changes like this definitely help make your code better .Happy coding 