Tuesday, 10 February 2015

Unnecessary Try Catch Block Affects Application Performance in C# & VB

It is quite common to use a try catch block to handle the exception thrown by the application. It can be for saving the exception details for debugging purposes or to hide the exception from displaying to the end users and replace it with a more user friendly messages. But blindly use it will affect the application performance.

Let's take a look of an example of how the application's performance get affected from adding unnecessary try catch block by comparing rethrow an exception back to caller & letting the exception flow back to the caller. We will do a performance comparison between both of them with a Stopwatch class.

I have a method that catch the exception and rethrow it back to caller without doing anything in the catch block.
[C#]
private void IntendedException()
{
    try
    {
        throw new ApplicationException("Throw exception with catch");
    }
    catch (ApplicationException)
    {
        throw;
    }
}

[VB]
Private Sub IntendedException()
    Try
        Throw New ApplicationException("Throw exception with catch")
    Catch ex As Exception
        Throw
    End Try
End Sub


This is another method that just let the exception flow back to caller.
[C#]
private void IntendedExceptionWithoutCatch()
{
    throw new ApplicationException("Throw exception without catch");
}

[VB]
Private Sub IntendedExceptionWithoutCatch()
    Throw New ApplicationException("Throw exception with catch")
End Sub

Then in the console application, we will do a 2000 loop and check how long it takes to complete the execution with Stopwatch.

[C#]
Stopwatch sw = new Stopwatch();

sw.Start();

for (int i = 1; i <= 2000; i++)
{
    try
    {
        IntendedException();
    }
    catch (Exception)
    {
        // Do stuff here.
    }
}

sw.Stop();
Console.WriteLine(string.Format("IntendedException {0}", sw.Elapsed.TotalMilliseconds));
sw.Reset();
sw.Start();

for (int i = 1; i <= 2000; i++)
{
    try
    {
        IntendedExceptionWithoutCatch();
    }
    catch (Exception)
    {
        // Do stuff here.
    }
}
sw.Stop();

Console.WriteLine(string.Format("IntendedExceptionWithoutCatch {0}", sw.Elapsed.TotalMilliseconds));
Console.ReadKey();

[VB]
Dim sw As Stopwatch = New Stopwatch()

sw.Start()

For i As Integer = 1 To 2000
    Try
        IntendedException()
    Catch ex As Exception
        ' Do something here.
    End Try
Next

sw.Stop()
Console.WriteLine(String.Format("IntendedException {0}", sw.Elapsed.TotalMilliseconds))
sw.Reset()
sw.Start()

For i As Integer = 1 To 2000
    Try
        IntendedExceptionWithoutCatch()
    Catch ex As Exception
        ' Do something here.
    End Try
Next

sw.Stop()
Console.WriteLine(String.Format("IntendedExceptionWithoutCatch {0}", sw.Elapsed.TotalMilliseconds))
Console.ReadKey()

Execute the above code will get the following result. (The result varies depending on the machine that it runs on, debug mode and release mode)
IntendedException 37.6998ms
IntendedExceptionWithoutCatch 18.7001ms

As you can see from the above result, it will perform better with lesser try catch block. But that does not mean that we have to scrap off all the try catch block in your code, just have to use it wisely and not simply use it in every section of your code. Based on the above example, if the catch is not doing anything, then just let it flow back to the caller to handle the exception.

Here's the source code: https://onedrive.live.com/redir?resid=E6612168B803803D!352&authkey=!AHy5NurawlsrM7Q&ithint=file%2czip




No comments:

Post a Comment