-1

I am working in a C# Desktop application
I had made some code such that on execution of that code it falls into the catch block

try
{

        //come code

}
catch(exception ex)
{

}
  //some code 2

where I have not handled the exception because i want to excecute the code outside the catch block
'some code 2'

but on unhandling the exception it decreases the performance of execution

so please any alter solution for this

Amit Bisht
  • 4,870
  • 14
  • 54
  • 83
  • some code 2 will be executed since you have a catch, and if the catch doesnt rethrow – Filip Sep 22 '13 at 16:01
  • In what kind of desktop application can the performance of exceptions matter? Can you write code to avoid the exception? – adrianm Sep 22 '13 at 16:08

3 Answers3

4

Don't use exceptions for normal program flow. Exceptions are not optimised for speed, because they are normally used in situations where the performance no longer matters.

When the exception is created, it gathers a lot of information that could be used to troubleshoot the error, so that takes some time. If you have compiled the code in debug mode it will collect even more information, taking even more time to do so.

Whatever you are doing that causes the exception, try to catch that condition before it causes the actual error. For example if you are catching a division by zero, you should check the value that you are dividing by before doing the division.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

You should restructure your code to avoid using exceptions for control flow.

Exceptions should be reserved for exceptional things. There is a performance overhead in throwing and catching an exception, that cannot be avoided. Much of the overhead comes from capturing the stack trace, call site and other info to put in the exception.

Also be aware, that debug mode adds significantly to the time take to process an exception. (So if you haven't tried it in Release mode yet, perhaps you should try that first - though the advice in the first line of this answer still apply).

See also Exceptions and Performance on MSDN; there are also good resources to be found in this previous question and answer: How expensive are exceptions in C#?.

Community
  • 1
  • 1
driis
  • 161,458
  • 45
  • 265
  • 341
1

Throwing and catching an exception does have an impact on performance, but that's not the biggest problem with your code. Swallowing an exception like this is bad practice, and it's also not a good idea to catch System.Exceptions. Generally speaking, you should only catch exceptions you can reasonably handle and allow anything else to bubble up. If you want to execute code after your try block, regardless of whether an exception occurs, consider using a finally block. For example:

try
{
    // some code
}
finally
{
    // some code 2
}

Or possibly you can reorganize your code to avoid using a try-catch or try-finally at all.

You may want to create a global exception handler for your app, most likely using the AppDomain.UnhandledException event, for logging, etc.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331