带过滤的C#异常监视器 [英] C# exception monitor with filtering
问题描述
我发现一些关于C#中的异常过滤的问题。我想为此写一些模拟。我不知道如何帮助某人,我没有使用VB过滤。当然,没有优化和生产准备,但工作和做什么必须做(我如何理解问题)。所以工作草图:
using System;
命名空间ExceptionFiltering
{
class ExceptionMonitor
{
Action m_action = null;
public ExceptionMonitor(Action action)
{
this.m_action = action;
}
public void TryWhere(Func< Exception,bool> filter)
{
try
{
this.m_action();
}
catch(Exception ex)
{
if(filter(ex)== false)
{
throw;
}
}
}
public static void TryWhere(Action action,Func< Exception,bool>过滤器)
{
try
{
action();
}
catch(Exception ex)
{
if(filter(ex)== false)
{
throw;
}
}
}
}
类程序
{
//简单过滤器template1
static Func< ;异常,bool> m_defaultExceptionFilter = ex =>
{
if(ex.GetType()== typeof(System.Exception))
{
return true;
}
return false;
};
//简单过滤器template2
static Func< Exception,bool> m_notImplementedExceptionFilter = ex =>
{
if(ex.GetType()== typeof(System.NotImplementedException))
{
return true;
}
return false;
};
static void Main(string [] args)
{
//创建异常监视器
ExceptionMonitor exMonitor = new ExceptionMonitor(()=>
{
//体验try catch块
throw new Exception();
});
//调用try catch body
exMonitor.TryWhere(m_defaultExceptionFilter);
//使用静态方法调用try catch body
ExceptionMonitor.TryWhere(()=>
{
// try catch块
抛出新的System.NotImplementedException();
},m_notImplementedExceptionFilter);
//可以像ExceptionMonitor.Try(action)一样的语法.Where(filter)
//可以使用过滤器数组
}
}
Todo:全局异常处理程序,支持多个过滤器等等;
感谢您的任何建议,更正和优化!!!
对不起,我是C#的新手,所以我无法理解你的代码,但是我仍然希望我的建议可以帮助。
if(somecondition)
{
return true;
}
else
{
return false;
}
可以简单地:
$ $ $ $ $ $ $ $ code>
您需要在调用 Action $ c $之前检查null c>:
if(m_action!= null)m_action();
I found some question about exception filtering in C#. And i think to write some emulation for it. I do not know how it helps for someone and i did not work with VB filtering. Of course it is not optimized and production ready, but works and do what must do (how i understood the problem). So working sketch:
using System;
namespace ExceptionFiltering
{
class ExceptionMonitor
{
Action m_action = null;
public ExceptionMonitor(Action action)
{
this.m_action = action;
}
public void TryWhere(Func<Exception, bool> filter)
{
try
{
this.m_action();
}
catch (Exception ex)
{
if (filter(ex) == false)
{
throw;
}
}
}
public static void TryWhere(Action action, Func<Exception, bool> filter)
{
try
{
action();
}
catch (Exception ex)
{
if (filter(ex) == false)
{
throw;
}
}
}
}
class Program
{
//Simple filter template1
static Func<Exception, bool> m_defaultExceptionFilter = ex =>
{
if (ex.GetType() == typeof(System.Exception))
{
return true;
}
return false;
};
//Simple filter template2
static Func<Exception, bool> m_notImplementedExceptionFilter = ex =>
{
if (ex.GetType() == typeof(System.NotImplementedException))
{
return true;
}
return false;
};
static void Main(string[] args)
{
//Create exception monitor
ExceptionMonitor exMonitor = new ExceptionMonitor(() =>
{
//Body of try catch block
throw new Exception();
});
//Call try catch body
exMonitor.TryWhere(m_defaultExceptionFilter);
//Call try catch body using static method
ExceptionMonitor.TryWhere(() =>
{
//Body of try catch block
throw new System.NotImplementedException();
}, m_notImplementedExceptionFilter);
//Can be syntax like ExceptionMonitor.Try(action).Where(filter)
//Can be made with array of filters
}
}
}
Todo: global exception handler, supporting of multiple filters etc;
Thank you for any advices, corrections and optimizations!!!
Sorry I'm new to C# so I can't understand your code, but still I hope my advice can help.
if (somecondition)
{
return true;
}
else
{
return false;
}
can be simply:
return somecondition;
And you need to check null before invoke an Action
:
if (m_action!=null) m_action();
这篇关于带过滤的C#异常监视器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!