Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I have following code snippet:

C#
private void DoSomething1()
        {
            try
            {
                Method1();
            }
            catch (Exception ex)
            {
                ProcessException(ex);
            }
        }

        private void DoSomething2()
        {
            try
            {
                Method2();
            }
            catch (Exception ex)
            {
                ProcessException(ex);
            }
        }


I would like to prevent try catch block to be duplicated. I remember it can be writen by using Lambda expressions. How to do it better?
Posted

One my friend offered to write in this manner:

C#
private void PackExceptionHandling(Action f)
        {
            try
            {
                f();
            }
            catch (Exception ex)
            {
                ProcessException(ex);
            }
        }


Then let's call methods where we want to catch an exception:

C#
...
PackExceptionHandling(Method1);
PackExceptionHandling(Method2);
...


Looks very good.
 
Share this answer
 
Comments
BobJanova 23-Mar-11 9:56am    
This is good as long as the code is not in a performance critical point in the app (delegate/action references do increase the overhead by a small amount).

Also, although you excellently answer the specific question asked by the OP, it is not great style to use a delegate simply to pack the exception handling. I suspect the original questioner may have a slightly higher level design issue as suggested by Mika below.
Sergey Alexandrovich Kryukov 23-Mar-11 18:02pm    
You should not catch Exception. Generally you should catch derived classes. See my Answer.
--SA
You could use a delegate. Set the delegate, and call a method that calls the delegate. As long as the method prototypes match, this should be a viable approach. I've used it and it works a treat.

Oops, looks like using Action is the same thing.
 
Share this answer
 
v2
Why not use single method with parameterization? Another possibility could be to move the try catch to the calling layer...
 
Share this answer
 
Comments
Pavel Yermalovich 23-Mar-11 8:06am    
How? I forgot how to do it
Wendelius 23-Mar-11 8:18am    
You're most like referring lambdas, but what I meant is that could you either:
DoSomething(int execNumber) {
try {
if (execNumber == 1) Method1();
if (execNumber == 2) Method2();
...

or

SomeMethod() {
try {
if (decision to call DoSomething1) DoSomething1();
else DoSomething2();
...

Or are these method totally unrelated?
The code in your sample usually makes no sense. What, you block propagation of exception up the stack? Usually, this is wrong. If you understand your exception handling strategy, the "reuse" problem will not appear at all. The common problem is using "try" to often, where you should simply let go and catch is much upper on stack.

Please read my directions here:
How do i make a loop that will stop when a scrollbar reaches the bottom[^]
When i run an application an exception is caught how to handle this?[^]

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900