-1

How does using Try-Catch blocks in C#/Java programs affect their execution speed?
I'm in the development process (using Visual C#) of writing a database app and have been putting a try-catch block in almost all methods that could possibly cause an error,
but was curious if doing so will reduce the execution speed of my program as well?

I know there are other factors that will affect speed as well, but does this slow the program down?

4
  • 2
    Exceptions are only costly when thrown as far as I am aware. If no exception is thrown.. there is no stack walk or anything.. code just runs. So if your question is purely related to the try/catch block itself.. then I would bet the answer is "they affect it by 0%". Commented Jan 3, 2014 at 5:08
  • @SimonWhitehead you could write that as an answer =) Commented Jan 3, 2014 at 5:08
  • 1
    The big point of try/catch is that you don't put it everywhere. You put it in places where you could logically handle the error, and otherwise let control bubble up to someplace that can. Commented Jan 3, 2014 at 5:09
  • possible duplicate of try catch performance Commented Jan 3, 2014 at 5:09

6 Answers 6

3

Normally it has no effect on code as long as no exception is thrown. When exception is thrown it usually slows up the application as it is a costly operation. However there is on very interesting discussion here which you should read about.

Sign up to request clarification or add additional context in comments.

Comments

0

Since your app is database-driven, I'd say that most likely the time taken by the database calls will dwarf any performance penalties introduced by the try/catch blocks. If try/catch is the appropriate construct for what you are trying to do, then by all means do so.

Comments

0

Not sure about Java, but in C#, the cost is extremely low, however, I question the logic of doing this. I prefer the Yoda method. Do or do not. There is no try.

What would you have the catch blocks do? If you are interested in logging, you can use exception filters (CATCH WHEN in VB.NET) to capture information about an exception as it is first occurring and use reflection to log all sorts of data.

Comments

0

try catch blocks are executed only when ur program gets error by getting unexpected input. otherwise it just used to set flag. so its not affect execution time or speed.

Comments

0

It affect when u unhandled exception and unhandled exception cause low performance
check this out may be its helpful Tyr/Catch

For example you had put some code on combobox Selected index change event

 private void cmbMedium_SelectedIndexChanged(object sender, EventArgs e)
        {
           //Some Code
        }  

And you set DataSource or Add Items in combobox on Form Load event than while setting DataSource or Adding Items cmbMedium_SelectedIndexChanged Event trigger
At this time some people put unhandled Exception on cmbMedium_SelectedIndexChanged like this

private void cmbMedium_SelectedIndexChanged(object sender, EventArgs e)
{
  Try
     {
       //Some Code
     }
  catach(Exception ex)
     {}
}  

Than this type of Unhandled Exception Causes Low performance

Comments

-1

Yes, Try/Catch affect your performance. For more information please check this link.

1 Comment

Your link does not say what you say it says. You are making a common error of conflating using try/catch with throwing exceptions. The first is basically free. The second is expensive.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.