@Blog.Author(Nandip Makwana) .LearningExperience(ASP.NET, ASP.NET MVC, IIS, jQuery & Technology Surrounding it...)

December 6, 2011 comment , , ,

Efficient Use of Try Catch Block

In all application, whether it is web or desktop, whether it is built in .NET or JAVA or any other language, we all are using (and we should use if we are not!) Try Catch Block or similar available mechanism to handle the exception.

But recently while I was working on one application, I observed that Try Catch Block was also used to make decision!!! As Try Catch mechanism is provided to handle exception and hence it is heavy operation and utilizes more CPU, we should not use Try Catch for such decision making scenario. Instead we should look for any alternate solution. Let try to understand whole scenario by formulating one small example.

In all below example, I am using System.Diagnostics.Stopwatch class to count CPU ticks consumed by particular task.

Example 1 Scenario 1 : With Try Catch

string strPrice = "abc";
int Price;           
Stopwatch sw = new Stopwatch();
sw.Start();
 
try
{
    Price = Int32.Parse(strPrice);
    // DO SOMETHING
}
catch (Exception ex)
{
    Price = 0;
    // DO SOMETHING
}
 
sw.Stop();
Console.WriteLine("CPU Ticks Used For Parsing : " + sw.ElapsedTicks.ToString());

Output:

In above example, we are parsing string into integer within Try block and performing some operation (If part of if else condition). And if we are unable to parse string (as displayed in above case), it throws an exception and goes in Catch block and there we perform some other operation (else past of if else condition).

Now observe below scenario, in that we are rewriting above 10 line code with another 10 line with if else and of course we are removing Try Catch also :)

Example 1 Scenario 2 : Without Try Catch

string strPrice = "abc";
int Price;           
Stopwatch sw = new Stopwatch();
sw.Start();
 
Int32.TryParse(strPrice, out Price);
if (Price == 0)
{
    // DO SOMETHING
}
else
{
    // DO SOMETHING
}
 
sw.Stop();
Console.WriteLine("CPU Ticks Used For Parsing : " + sw.ElapsedTicks.ToString());

Output:

Volia!!! We have reduced CPU ticks from 42949 to 27.

I don’t think that, I need to write more on scenario 2. It is self explanatory. Here the idea is that we should use Try Catch to handle exception only. In above example we are just parsing one string, but consider scenario where we are iterating through few thousands element or collection and where 90% or more elements are suppose to be string only and hence it goes in Catch block (Yes I faced the same situation!) After replacing Try Catch Block with If Else, I observed 10 times faster code than previous one.

Now let see one more example for illustrative purpose.

Example2 Scenario 1 : With Try Catch

Stopwatch sw = new Stopwatch();
sw.Start();
 
FileStream fs;
try
{
    fs = File.Open("D:\\nandip.docx", FileMode.Open);
}
catch
{
    fs = File.Open("D:\\nandip.docx", FileMode.Create);
}
 
sw.Stop();
Console.WriteLine("CPU Ticks Used For Reading File : " + sw.ElapsedTicks.ToString());

Output:

Example 2 Scenario 2 : Without Try Catch

Stopwatch sw = new Stopwatch();
sw.Start();
 
if (File.Exists("D:\\nandip.docx"))
{
    fs = File.Open("D:\\nandip.docx", FileMode.Open);
}
else
{
    fs = File.Open("D:\\nandip.docx", FileMode.Create);
}
 
sw.Stop();
Console.WriteLine("CPU Ticks Used For Reading File : " + sw.ElapsedTicks.ToString());

Output:

In above example also we can see CPU ticks reducing from 106668 to 1820. Idea behind above example is to show how to choose alternate solution where we fall in such situation or found in inherited code :)

I hope two example discussed here, one real life example and one for illustrative purpose, would be helpful.

comments powered by Disqus

Featured Content

Resources & Tools

About Nandip Makwana

Nandip Makwana is passionate about digital world and web. He completed his Masters in Computer Application in June 2011. Currently he is working as a Software Engineer. He has shown great promise and command over ASP.NET and technologies surrounding it during his academic years and professorial life...continue reading