Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在CSharp中捕获类级别的所有异常?_C#_Exception_Exception Handling - Fatal编程技术网

C# 如何在CSharp中捕获类级别的所有异常?

C# 如何在CSharp中捕获类级别的所有异常?,c#,exception,exception-handling,C#,Exception,Exception Handling,我有一门课,像: class SampleRepositoryClass { void MethodA() { try { //do something } catch(Exception ex) { LogError(ex); throw ex; } } void MethodB

我有一门课,像:

class SampleRepositoryClass
{
    void MethodA()
    {
        try
        {
            //do something
        }
        catch(Exception ex)
        {
            LogError(ex);
            throw ex;
        }        
    }

    void MethodB(int a, int b)
    {
        try
        {
            //do something
        }
        catch(Exception ex)
        {
            LogError(ex);
            throw ex;
        }
    }

    List<int> MethodC(int userId)
    {
        try
        {
            //do something
        }
        catch(Exception ex)
        {
            LogError(ex);
            throw ex;
        }
    }
}
类采样器位置类
{
void MethodA()
{
尝试
{
//做点什么
}
捕获(例外情况除外)
{
对数误差(ex);
掷骰子;
}        
}
无效方法b(内部a、内部b)
{
尝试
{
//做点什么
}
捕获(例外情况除外)
{
对数误差(ex);
掷骰子;
}
}
列表方法C(int userId)
{
尝试
{
//做点什么
}
捕获(例外情况除外)
{
对数误差(ex);
掷骰子;
}
}
}
在上面的示例中,您可以看到每个方法(MethodA、MethodB、MethodC)都有try…catch块来记录错误,然后抛出到更高的级别

想象一下,当我的Repository类可能有上百个方法,并且在每个方法中我都尝试了…catch块,即使只有一行代码


现在,我的目的是减少这些重复的异常记录代码,并在类级别而不是方法级别记录所有异常。

您太过保守了。不要过度使用
try..catch
只在需要的地方使用


在这种情况下,考虑通过与类之外的类交互来抛出异常。请记住,异常将被传播。

使用诸如策略注入应用程序块、Castle、Spring.NET等库。这些库允许您将行为注入异常捕获。

只需在App.xaml.cs中实现DispatcherUnhandledException;它将处理您的所有异常

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            DispatcherUnhandledException += App_DispatcherUnhandledException;
        }
        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            LogError(e);
// MessageBox.Show(e.Exception.Message);
            e.Handled = true;
        }

既然有免费的东西,为什么要重新发明轮子呢。 只需添加
PostSharp.dll
作为项目的参考即可。 完成此操作后,您的存储库将如下所示:

[Serializable]
class ExceptionWrapper : OnExceptionAspect
{
    public override void OnException(MethodExecutionArgs args)
    {
        LogError(args.Exception);
        //throw args.Exception;
    }
}

[ExceptionWrapper]
class SampleRepositoryClass
{
    public void MethodA()
    {
        //Do Something
    }

    void MethodB(int a, int b)
    {
        //Do Something
    }

    List<int> MethodC(int userId)
    {
        //Do Something
    }
}
[可序列化]
类ExceptionWrapper:OneExceptionSpect
{
public override void OnException(MethodExecutionArgs args)
{
日志错误(参数异常);
//抛出args.Exception;
}
}
[例外包装器]
类采样器位置类
{
公开无效方法a()
{
//做点什么
}
无效方法b(内部a、内部b)
{
//做点什么
}
列表方法C(int userId)
{
//做点什么
}
}
在类上添加
ExceptionWrapper
属性,确保所有属性和方法都封装在try/catch块中。catch中的代码将是您放入ExceptionWrapper中重写的函数OnException()中的代码


您也不需要编写代码来重播。如果提供了正确的流行为,也可以自动重新引发异常。请查看相关文档。

只有工具/织布工可以帮助您。顺便说一下,一个类中有100多个方法?那只是。。。糟糕。至少从我的角度来看。这确实很可怕,但是我的业务和存储库类有很多方法,这使得这些类太重了。实际上,一个类中方法的数量取决于业务规则的复杂性或要执行的操作的数量。不要使用“throw ex;”,因为这会破坏宝贵的调用堆栈。只要写“throw”;异常中的信息就会被保留。话虽如此,我想指出,除非您知道如何处理异常,否则不应该捕获异常。参考资料:@Patrik-我知道我应该写“扔”。您的建议是实施错误处理的最佳实践。但我的情况不同,我希望在单个类级别处理所有类方法异常。对于类A中的所有方法,必须只有一个位置来处理和记录类A中任何方法中发生的错误。抱歉,1月1日,我想在BAL或DAL的类库中执行此操作。请在不使用任何其他库(策略注入应用程序块、Castle或Spring.NET)的情况下共享一些示例代码,好吗?不幸的是,这些库实现了代理设计模式,并处理了大量的反射等。如果我跳过这个管道,我将不得不编写大量代码。。。