Beautiful wrapper for try-catch in C#

Question:

The question is not about how to program beautifully so that this question does not arise, but about a beautiful wrapper over try-catch .

try
{

}
catch 
{
    // здесь ничего нет
}
  • this is a good thing, but it takes up a lot of space, I would like it to fit in one block, but perform the same functionality

I am making a piece of code that is a safety net and not worthy of much attention at all (now I pay such attention only for the future, because I have already thought about this issue more than once, well, to relax). It's not worth the refactoring to be elegant. It should be simple and quick to put try , but I would like it to look nice. If something doesn’t work there, then it’s okay, but I would also like it not to lead to the appearance of an Exception already in important places, so the question is about try-catch .

There is an option to make a method:

private void Try(Action codeBlock)
{
    try
    {
        codeBlock?.Invoke();
    } 
    catch 
    {
    } 
}

then the call will be in one line, but this option is not 100% like:

  • it seems not nice to call Try( ()=>DoWork() ); , I would like something simpler, without bells and whistles

  • you can pass the method call directly to the argument Try(MethodCall) , it already looks better, but then in my particular case I will have to break this safety net and a method of secondary importance into many parts, for example, if the case is like this:

     private static void EnsureSomethingWhichFailsAnyway(Someting input) { try { foreach (var x in input.GetAllX()) { DoSmallThing(x); try { x.SetPropertyValue = PossibleValues.BigValue; } catch { } } } catch { } try { foreach (var y in input.GetllY()) { try { y.Validate(StaticVars.A, StaticVars.B, StaticVars.C); } catch { } } } catch { } }
  • that is, if you break such a method into submethods to call the Try(MethodCall) type, then the game will not be worth the candle.

  • is it possible to somehow put in using ?

  • maybe there is no ideal solution, then it's interesting just to hear constructive thoughts.

Thanks!

Answer:

I pecked at an idea that allows you to fit in one block.

You can use it like this:

    Try.AutoRunAction = () =>
    {
        //code block
    };

Here is the raw implementation:

    public class Try
    {
        // runs on set
        public static Action AutoRunAction
        {
            set
            {
                try { value?.Invoke(); }
                catch { }
            }
        }
    }
  • same as from method, but less parentheses
  • used to be a getter, which was advised to remove
  • can also be called AutoTryAction
  • accept criticism

Example:

    private static void SetAttributesNormal(DirectoryInfo dir)
    {
        AutoTryAction = () =>
        {
            foreach (var subDir in dir.GetDirectories())
            {
                SetAttributesNormal(subDir);
                AutoTryAction = () => subDir.Attributes = FileAttributes.Normal;
            }
        };

        AutoTryAction = () =>
        {
            foreach (var file in dir.GetFiles())
            {
                AutoTryAction = () => file.Attributes = FileAttributes.Normal;
            }
        };
    }

    private static Action AutoTryAction
    {
        set
        {
            try { value?.Invoke(); }
            catch { }
        }
    }
Scroll to Top