一、方法1:封装一个包含try{}catch{}finally{}的异常处理逻辑的方法类,将别的方法作为参数传入该方法,在页面内调用封装后的方法,大体代码如下:
public class Process
{
public static bool Execute(Action action)
{
try
{
action.Invoke();
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
}
}
}
使用方式
public ActionResult Index(int id)
{
Boolean result = false; //接收返回的值
Process.Execute(() => result = Save(id)); //执行方法
return View();
}
或
public ActionResult Index()
{
Process.Execute(()=>
{
... //更为复杂的逻辑
});
return View();
}