4

Sorry if my question is stupid, but I have this kind of code :

public Object1 Method1(Object2 parameter) { try { return this.linkToMyServer.Method1(parameter); } catch (Exception e) { this.Logger(e); } return null; } public Object3 Method2(Object4 parameter) { try { return this.linkToMyServer.Method2(parameter); } catch (Exception e) { this.Logger(e); } return null; } /* ... */ public ObjectXX Method50(ObjectXY parameter) { try { return this.linkToMyServer.Method50(parameter); } catch (Exception e) { this.Logger(e); } return null; } 

I think you see the pattern. Is there a nice way to have only one try catch and to pass a generic method in this try catch ?

Instinctively I'd use a delegate, but delegates have to have the same signature right ?

Thanks in advance.

Regards.

3 Answers 3

9

Whenever you see code like this you can apply Template Method Pattern.

May be something like this:

private TResult ExecuteWithExceptionHandling<TParam, TResult>(TParam parameter, Func<TParam, TResult> func) { try { return func(parameter); } catch (Exception e) { this.Logger(e); } return default(TResult); } public Object1 Method1(Object2 parameter) { return ExecuteWithExceptionHandling(parameter, linkToMyServer.Method1); } public Object3 Method2(Object4 parameter) { return ExecuteWithExceptionHandling(parameter, linkToMyServer.Method2); } 

And so on...

Sign up to request clarification or add additional context in comments.

3 Comments

This, or just get a central point to manage the final handling of your exceptions; meaning, if you have to log a lot, you can catch them in your functions, and rethrow them to handle them in Main or else. Maybe mind throwing a new exception though, with the previous one as its InnerException property.
@Sriram, hey! Mind blowing code, I've implemented it in my custom pattern. But question: How to use that on voids and parameterless methods? It causes this error: Severity Code Description Project File Line Suppression State Error CS0411 The type arguments for method 'BLLFactory.Execute<TParam, TResult>(TParam, Func<TParam, TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
@Eli Hard to tell without looking at your code. For void methods you've to create another overload of your template method which takes Action as a parameter not Func. If you still can't get it working please create new question and optionally drop a link here.
1

This might be useful to you.

public object BaseMethod(object[] userParameters,String FunctionName) { try { Type thisType = this.GetType(); MethodInfo theMethod = thisType.GetMethod(FunctionName); object returnObj; returnObj = theMethod.Invoke(this, userParameters); return returnObj; } catch (Exception e) { this.Logger(e.InnerException); } } 

Comments

0

One example of such central handling can be found from chocolatey open source git.

Centralization happens using two static public methods FaultTolerance.try_catch_with_logging_exception - for example user of this function would need to write something like this:

ChocolateyPackageService.cs#L1073

and there are two functions which handles this kind of call -

  1. With Func parameter

  2. With Action parameter

Other arguments to function are up to you to decide if you want to have them or not - they could adjust or log specific error message once exception occurs.

(All links are provided with absolute points in git history, so will not get broken in future).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.