Can an area block with the use keyword respond to exceptions?

I need to make some entries in my code. I have to use the internal library developed by the company to record some information. Here's how it works.

Recorder recorder = Recorder.StartTiming();
DoSomeWork();
recorder.Stop();  // Writes some diagnostic information.

To ensure that Stop () is always called, I created a wrapper class that allows you to use a clean "using" block.

using (RecorderWrapper recorderWrapper = new RecorderWrapper)  // Automatically calls Recorder.StartTiming() under the covers
{
   DoSomeWork();
}  // When the recorderWrapper goes out of scope, the 'using' statement calls recorderWrapper.Dispose() automatically - which calls recorder.Stop() under the covers

he worked so far. However, there is a change that my company requires, which will look something like this on the source code:

Recorder recorder = Recorder.StartTiming();
try
{
   DoSomeWork();
}
catch (Exception ex)
{
   recorder.ReportFailure(ex);  // Write out some exception details associated with this "transaction"
}
recorder.Stop();  // Writes some diagnostic information.

I would like to avoid trying / catching in all of my blocks of scope using RecorderWrapper. Is there a way to place a call to “ReportFailure ()” and still use the “use” block of the scope?

, , " ", .. . , recorder.Stop() try/catch.

!

+3
8

, , :

public void Record(Action act)
{
    try
    {
        this.StartTiming();
        act();
    }
    catch(Exception ex)
    {
        this.ReportFailure(ex);
    }
    finally
    {
        this.Stop();
    }
}

, :

recorder.Record(DoSomeWork);
+7

- :

280Z28: StartNew(), Stopwatch.StartNew(). Recorder IDisposable Stop() Dispose(). , , .

using (Recorder recorder = Recorder.StartNew())
{
    try
    {
        DoSomeWork();
    }
    catch (Exception ex)
    {
        recorder.ReportFailure(ex);
    }
}
+3

RecorderWrapper, , TryExecuting, , , try/catch. :

using (RecorderWrapper recorderWrapper = new RecorderWrapper)  // Automatically calls Recorder.StartTiming() under the covers
{
    recorderWrapper.TryExecuting(() => DoSomeWork());
}

Inside RecorderWrapper:

public void TryExecuting(Action work)
{
    try { work(); }
    catch(Exception ex) { this.ReportFailure(ex); }
}
+3

, TransactionScope, , , - Complete(), Dispose() ( ) :

using(Recorder recorder = Recorder.StartTiming()) {
    DoSomeWork();
    recorder.Complete();
}

try/catch - - Exception.

+1

, try/finally. try/catch. , , .

+1

try/finally, dispose .

, :

using(a = new A())
{
    a.Act();
}

( , ) :

a = new A();
try
{
    a.Act();
}
finally
{
    a.Dispose();
}

try.

Edit:

Rob:

Recorder recorder = Recorder.StartNew()
try
{
    DoSomeWork();
}
catch (Exception ex)
{
    recorder.ReportFailure(ex);
}
finally
{
    recorder.Dispose();
}
+1

, , StartTiming Recorder. . Wrap Recorder, , , , , .

, - , lambdas, Action :

:

public static class RecorderScope
{
   public static void Wrap(Action<Recorder> action)
   {
      Recorder recorder = Recorder.StartTiming();
      try
      {
         action(recorder);
      }
      catch(Exception exception)
      {
         recorder.ReportFailure(exception);
      }
      finally
      {
         recorder.Stop();
      }
   }
}

, :

RecorderScope.Wrap(
   (recorder) =>
   {
      // note, the recorder is passed in here so you can use it if needed -
      // if you never need it you can remove it from the Wrap function.
      DoSomeWork();
   });

: , , ? .

, , . , , , : - - , , . , Wrap, Action T Exception, , catch , :

public static class RecorderScope
{
   public static void Wrap(Action<Recorder> action, 
      Action<Recorder, T1> exHandler1)
      where T1: Exception
   {
      Recorder recorder = Recorder.StartTiming();
      try
      {
         action(recorder);
      }
      catch(T1 ex1)
      {
         exHandler1(recorder, ex1);
      }
      finally
      {
         recorder.Stop();
      }
   }
}

.. ( , , , , . , ):

RecorderScope.Wrap(
   (recorder) =>
   {
      DoSomeWork();
   },
   (recorder, MyException ex) =>
   {
      recorder.ReportFailure(exception);
   });

, Wrap, . - .

+1

. , try..catch..finally Dispose() finally.

0

Source: https://habr.com/ru/post/1723032/


All Articles