Bijay Yadav
Bijay Yadav

Reputation: 958

Create a method which catches exception if occurred in any of the methods in that class in c#

I have a class Demo which has four methods as below Add(), Update(), Delete() and Get().

And these methods are chained together as below:

bool isSuccess = this.Get(1)
                    .Update(200) //new product price

Now, I want to implement CatchError() method which is going to catch the Exception if occurred in any of the above methods.

The code would look as below:

bool isSuccess = this.Get(1)
                    .Update(200); //new product price
                    .CatchError();

I have no idea how to implementCatchError() method.

Would be happy to provide additional information to assist the questions properly.

Upvotes: 3

Views: 166

Answers (3)

Cleptus
Cleptus

Reputation: 3541

If you create a method in a fluent API to catch the exception you would also need to decide if you want to continue the execution of the fluent calls. Code following.

If creating a fluent method is not required, I would suggest Presis's answer because is less complex.

class Program
{
    static void Main(string[] args)
    {
        Car myCar = new Car();
        bool processCancelled = false;
        myCar.SubscribeOnError(CarOnError).Paint(ref processCancelled, "red").Sell(ref processCancelled, 25000d);
    }

    public static bool CarOnError(Exception e)
    {
        // Log exception
        // Decide if must cancel
        return true;
    }
}

public class Car
{
    private Func<Exception, bool> onErrorMethod = null;

    public Car SubscribeOnError(Func<Exception, bool> methodToCall)
    {
        onErrorMethod = methodToCall;
        return this;
    }
    public Car Paint(ref bool cancelled, string color)
    {
        if (cancelled) return this;
        try
        {
            // Do stuff
        }
        catch (Exception exc)
        {
            cancelled = onErrorMethod == null ? true : onErrorMethod(exc);
        }
        return this;
    }
    public Car Sell(ref bool cancelled, double price)
    {
        if (cancelled) return this;
        try
        {
            // Do stuff
        }
        catch (Exception exc)
        {
            cancelled = onErrorMethod == null ? true : onErrorMethod(exc);
        }
        return this;
    }
}

Upvotes: 1

Presi
Presi

Reputation: 827

Like I wrote in my comment I would try to create a method CatchError() where you give an Action as parameter:

bool isSuccess = CatchError(() => Get(1).Update(200));

Your CatchError() method would look like this:

private static bool CatchError(Action action)
{
    try
    {
        action();
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

Upvotes: 3

Maksim Simkin
Maksim Simkin

Reputation: 9679

To achieve that you should execute all operations only if CatchError is called. Till it, you should collect all actions that have to be performed. Something like:

public class Demo
{
    private int Value;
    private List<Action> Operations = new List<Action>();
    public Demo Get(int a)
    {
        this.Operations.Add(() => this.Value = a);
        return this;
    }

    public Demo Update(int a)
    {
        this.Operations.Add(() => this.Value += a);
        return this;
    }

    public bool CatchError()
    {
        foreach (var operation in Operations)
        {
            try
            {
                operation();
            }
            catch (Exception e)
            {
                return false;
            }
        }
        Operations.Clear();
        return true;
    }
}

Upvotes: 1

Related Questions