如果在 c# 中的 class 中的任何方法中发生,则创建一个捕获异常的方法
Create a method which catches exception if occurred in any of the methods in that class in c#
我有一个 class Demo
,它有以下四种方法 Add()
、Update()
、Delete()
和 Get()
。
这些方法链接在一起如下:
bool isSuccess = this.Get(1)
.Update(200) //new product price
现在,我想实现 CatchError()
方法,该方法将捕获上述任何方法中发生的 Exception if
。
代码如下所示:
bool isSuccess = this.Get(1)
.Update(200); //new product price
.CatchError();
我有no idea
如何实现CatchError()
方法。
很乐意提供更多信息以适当地帮助回答问题。
为此,您应该仅在调用 CatchError 时执行所有操作。直到它,你应该收集所有必须执行的操作。
类似于:
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;
}
}
就像我在评论中写的那样,我会尝试创建一个 CatchError() 方法,您可以在其中提供一个 Action 作为参数:
bool isSuccess = CatchError(() => Get(1).Update(200));
您的 CatchError() 方法如下所示:
private static bool CatchError(Action action)
{
try
{
action();
return true;
}
catch (Exception)
{
return false;
}
}
如果您在流畅的 API 中创建一个方法来捕获异常,您还需要决定是否要继续执行流畅的调用。代码如下。
如果不需要创建流畅的方法,我会建议 因为它不那么复杂。
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;
}
}
我有一个 class Demo
,它有以下四种方法 Add()
、Update()
、Delete()
和 Get()
。
这些方法链接在一起如下:
bool isSuccess = this.Get(1)
.Update(200) //new product price
现在,我想实现 CatchError()
方法,该方法将捕获上述任何方法中发生的 Exception if
。
代码如下所示:
bool isSuccess = this.Get(1)
.Update(200); //new product price
.CatchError();
我有no idea
如何实现CatchError()
方法。
很乐意提供更多信息以适当地帮助回答问题。
为此,您应该仅在调用 CatchError 时执行所有操作。直到它,你应该收集所有必须执行的操作。 类似于:
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;
}
}
就像我在评论中写的那样,我会尝试创建一个 CatchError() 方法,您可以在其中提供一个 Action 作为参数:
bool isSuccess = CatchError(() => Get(1).Update(200));
您的 CatchError() 方法如下所示:
private static bool CatchError(Action action)
{
try
{
action();
return true;
}
catch (Exception)
{
return false;
}
}
如果您在流畅的 API 中创建一个方法来捕获异常,您还需要决定是否要继续执行流畅的调用。代码如下。
如果不需要创建流畅的方法,我会建议
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;
}
}