ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

Use try catch only when necessary to run your application faster

.Net C# about 8 years ago || 8/26/2018 || 1.5 K View

  • Hide

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?

  1. 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.

  2. 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.

  3. 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.)

  4. The most specific Exception types should appear first in the structure, followed by the more general Exception types.

  5. 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
  

 

  • 1
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Alok Pandey

Alok Pandey

Love to share what I know.

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey