In my application i have 100 classes
and each class contains 4 methods
each.
I am using try catch
for exception handling. I write try catch in each method , then there will be 400 try catch statement
in my application. Is it affect the performance of my application? Is it possible to handle the whole exception using one try catch statement in the whole application
?

- 11,481
- 34
- 98
- 138
-
is it a asp.net app? then try something like ELMAH. and remove the try catch blocks from every method.allow the error to bubble up an then catch it.. – Ashley John Aug 10 '11 at 10:20
-
Related: http://stackoverflow.com/q/409563/38206 – Brian Rasmussen Aug 10 '11 at 10:21
-
Can you show some code that shows how you do this handling in each method? – Steven Aug 10 '11 at 10:26
6 Answers
Do you need to handle exceptions within each method? Typically that's not the way things work - in my experience, most exceptions can't really be handled in a better way than telling the user something went wrong and aborting the higher-level action, which could involve a stack of several methods.
So normally you'll have a very few try/catch blocks at the "top" level, and the methods doing most of the work will just let the exceptions bubble up.

- 1,421,763
- 867
- 9,128
- 9,194
-
1I would add that exception handlers on important boundaries makes debugging systems a lot easier. – Preet Sangha Aug 10 '11 at 10:22
-
@Jon Do you really want Catch-Handlers where you only want to show a Message to the User? I think then you better use try-finally and handle your Message Stuff in Application.ThreadException etc. – Skomski Aug 10 '11 at 10:30
-
@Skomski: It depends on what exactly you're doing. I wouldn't want to use `Application.ThreadException` in various situations as there may well already be a "catch all" high up which stops it ever reaching `ThreadException`. For example, ASP.NET won't let an exception reach the *very* top of the stack. There will *usually* be a top-level handler *somewhere*. It may be yours, or it may be someone else's. My main point is that you don't want to catch the exception *everywhere*. – Jon Skeet Aug 10 '11 at 10:33
there will be 400 try catch statement in my application
That is completely unnecessary and in fact bad, making your code overcomplicated and inefficient. Catch exceptions only where you can actually handle them.
Is it possible to handle the whole exception using one try catch statement in the whole application?
That would usually be the other extreme. Try to find the middle ground, identifying the places where you can meaningfully handle specific exceptions by e.g. logging, displaying an appropriate error message on the GUI, retrying the problematic action in a different way etc. If you are tempted to wrap an exception into a new, different exception and throw that to a higher level, it is often a sign that you may be catching it on too low a level. (The exception is when you need to encapsulate exceptions thrown by some lower level API to avoid dependencies.)

- 114,404
- 31
- 268
- 329
Catch exceptions as local as you're willing and able to handle them. A mere try
/catch
will not affect performance (it's just a jump target, after all), and if at all, then negligible. If exceptions get thrown then you'll have a performance hit, since exceptions are quite slow. But apart from that, no.

- 344,408
- 85
- 689
- 683
You should not use try-catch
anywhere in the class if that is behaving like a class library, use try-catch
where you are calling functions. That way it will be more managible and makes sense

- 5,758
- 5
- 25
- 39
If you have a large application and want not to clutter your code with try catch
you might consider using Enterprise Library Exception Handling Application Block
Instead of repeating this code (such as code that logs exception information) in identical catch blocks throughout an application, the Exception Handling Application Block allows developers to encapsulate this logic as reusable exception handlers.

- 7,593
- 2
- 36
- 52
Usually exceptions should be categorized at the lower levels of the application and re-thrown onto the upper levels, for example if it's a invalidity of business logic (say price of a product calculated as negative) is desired to categorized as different type of exception while error data loading should be categorized as some other type. Based on the type the exceptions can be handled in the top layer and depending on the type of error user can be informed what sort of action to be taken.

- 5,974
- 1
- 32
- 43