We always need to try catch in our code and it becomes ugly like
public void foo()
{
try
{
DoSomething();
}
catch(Exception e)
{
//do whatever with e
}
}
public int FooReturnInt()
{
try
{
return IntAfterSomeCalculation();
}
catch(Exception e)
{
//do exactly whatever with e as foo()
}
}
Imagine we have a huge class with many public functions like this and we have to apply same try catch in every single function.
Ideally, since the try catch part is identical, and we can pass down Func<> as parameter to a helper function which does something like
public void TryCatch(Func<something> theFunction)
{
try
{
theFunction();
}
catch(Exception e)
{
//processthe common exception
}
}
Then I'd imagine this would tidy up my code alot, the now the problem is how to properly write this function? The return type of this function is depended on the return type of theFunction.