Nithesh Narayanan
Nithesh Narayanan

Reputation: 11775

Avoiding multiple try catch

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?

Upvotes: 2

Views: 3451

Answers (6)

Low Flying Pelican
Low Flying Pelican

Reputation: 6054

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.

Upvotes: 0

George Mamaladze
George Mamaladze

Reputation: 7931

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.

Upvotes: 0

Péter Török
Péter Török

Reputation: 116306

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

Upvotes: 2

Prashant Lakhlani
Prashant Lakhlani

Reputation: 5806

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

Upvotes: 0

Joey
Joey

Reputation: 354794

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.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503290

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.

Upvotes: 7

Related Questions