Use try catch only when necessary to run your application faster
After reviewing codes of several projects what we found that developer just uses try catch and never think the cost of try catch on application performance. So today we tried to provide some facts that will help us to think that we should use try catch only where is it is necessary.
As we are discussing the performance of try catch so just want to explain about
What is Try Catch?
"Try" and "catch" are keywords that represent the handling of exceptions due to data or coding errors during program execution. A try block is the block of code in which exceptions occur. A catch block catches and handles try block exceptions.
Try catch block examples include:
- A try block followed by a catch block,
- A try block followed by one or more catch blocks,
- A try block followed by another try block and then followed by a corresponding catch block.
How try and catch work?
-
When an Exception is thrown by a statement in the try{} block, the catch{} blocks are examined one-by-one starting starting with the first.
-
The first catch{} block to match the type of the Exception gets control. In the above diagram, E1, E2, and E3 represent different types of exceptions. For example, E2 may be IOException and E1 maybe IndexOutofRange etc.
-
Only one catch{} block gets control. If no catch{} block matches the Exception, none is picked, and execution leaves this method (just as if there were no try{} block.)
-
The most specific Exception types should appear first in the structure, followed by the more general Exception types.
-
The statements in the chosen catch{} block execute sequentially. After the last statement executes, control goes to the first statement that follows the try/catch structure. Control does not return to the try block.
So to show the effect of try catch we created one small function to calculate tangent of given slope with the help of Math class.
public static double CalculateTangent(double slope)
{
return Math.Tan(slope);
}
Then with a simple console program, we are calling it repeatedly with try catch block and one with without try catch block. What we can see the block which is without try catch block is running faster than try catch block.
The difference can be seen in the given image below or you can self-explore in https://dotnetfiddle.net/dENsxH.
So with this fact its good to know how to use and when to use try catch block in order to make a good application. Best practices for handling and creating exceptions are listed below,
- Use try/catch/finally blocks
- Handle common conditions without throwing exceptions
- Design classes so that exceptions can be avoided
- Throw exceptions instead of returning an error code
- Use the predefined .NET exception types.
To know more details you can visit Best practices for exceptions https://docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions
Hope after watching this performance impact while using try catch we developer try to avoid try catch when it is not required.
Thank You