与过滤C#的异常监测 [英] C# exception monitor with filtering

查看:108
本文介绍了与过滤C#的异常监测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些问题约在C#中的异常过滤。而且我觉得写一些仿真它。我不知道它如何帮助的人,我没有用VB筛选工作。当然,这不是最优化的生产准备,但是工作做什么,必须做(我如何理解的问题)。所以工作草图:

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:多重过滤的全局异常处理程序,支持等;

Todo: global exception handler, supporting of multiple filters etc;

感谢您的任何意见,更正和优化!

Thank you for any advices, corrections and optimizations!!!

推荐答案

对不起我是新的C#,所以我无法理解你的代码,但还是希望我的建议能有所帮助。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆