0

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?

Amit Bisht
  • 4,870
  • 14
  • 54
  • 83
  • 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%". – Simon Whitehead Jan 03 '14 at 05:08
  • @SimonWhitehead you could write that as an answer =) – Federico Berasategui Jan 03 '14 at 05: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. – cHao Jan 03 '14 at 05:09
  • possible duplicate of [try catch performance](http://stackoverflow.com/questions/1350264/try-catch-performance) – Adarsh Shah Jan 03 '14 at 05:09

6 Answers6

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.

Community
  • 1
  • 1
Ehsan
  • 31,833
  • 6
  • 56
  • 65
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.

Katsuyuki Omuro
  • 597
  • 3
  • 8
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.

Hafthor
  • 16,358
  • 9
  • 56
  • 65
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.

vino20
  • 429
  • 4
  • 13
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

Community
  • 1
  • 1
Amit Bisht
  • 4,870
  • 14
  • 54
  • 83
-1

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

Jayesh Goyani
  • 11,008
  • 11
  • 30
  • 50
  • 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. – Andrew Barber Jan 03 '14 at 05:34