N
N
ndbn2016-05-23 15:00:31
.NET
ndbn, 2016-05-23 15:00:31

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

Those. we need a function that allows you to wrap any delegate or function in your own exception handler.
Show a similar construction, I don’t understand how to describe the IgnoreException function .
Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
Fat Lorrie, 2016-05-23
@ndbn

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
Iron Bug, 2016-05-23
@rare

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/

#
#algooptimize #bottize, 2016-05-23
@user004

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 question

Ask a Question

731 491 924 answers to any question