Answer the question
In order to leave comments, you need to log in
How to make a wrapper function to ignore exceptions?
Hello.
Some time ago I came across a construction of the form:
void IgnoreException( funcName? )
{
try{ funcName(); }
catch(Exception e){}
}
...
IgnoreException(
() => (
some code
)
);
Answer the question
In order to leave comments, you need to log in
interface ILogger {
void LogException(Exception ex);
}
...
void IgnoreExceptions<ExType>(Action func, ILogger logger) where ExType : Exception {
try {
func();
}
catch(ExType ex) {
logger.LogException(ex);
}
}
I recommend you use monads. There is roughly what you are trying to do (the try monad) + more. Here is the first one that came across from Google: https://github.com/louthy/csharp-monad#try-monad
It is also in the form of a nuget package: https://www.nuget.org/packages/csharp-monad/
If the terms lambda, delegates, Action, Func are not clear, then study the question.
What is the function, if not a secret, where you need to completely ignore the result?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question