简体   繁体   English

异常处理中的设计模式

[英]Design Patterns in Exception Handling

I am actually writing a library class which could be used by multiple classes. 我实际上正在编写一个可供多个类使用的库类。 I am simplying the example so as to make a point. 我简化了这个例子,以便说明一点。 Lets say I have three classes: A, B and C: 假设我有三个类:A,B和C:

public class B
{
  public static string B_Method()
  {
    string bstr = String.Empty;

    try
       {


        //Do Something

       }

    catch
        {

        //Do Something

        }

  return bstr;

}

B is the library class that I am writing. B是我正在写的库类。 Now there are lets say two other classes say A and C: 现在可以说其他两个类说A和C:

public class A
{
  public void A_Method()
  {
   string astr = B.B_Method();
  }

}

public class C
{
 public void C_Method()
  {
   string cstr = B.B_Method();
  }

}

The question is regarding the exception handling. 问题是关于异常处理。 I want the respective method of the two classes A and B to handle the exception occuring in B_Method in their own different ways. 我希望两个类A和B的相应方法以它们自己的不同方式处理B_Method中发生的异常。

I looked for framework design pattern, but felt that was not useful. 我寻找框架设计模式,但觉得没用。

The approach that I usually follow is this: 我通常遵循的方法是:

  • If my method can do something useful with the exception, I catch it and do that. 如果我的方法可以做一些有用的异常,我抓住它并做到这一点。
  • If not, I don't catch it and leave it up to calling code to deal with it. 如果没有,我不会抓住它并将其留给调用代码来处理它。

The only places where I would put a "catch all" block, is at entry points in UI code (such as event handlers for button clicks and such), since not catching exceptions there might take the process down. 我将放置“全部捕获”块的唯一地方是UI代码中的入口点(例如按钮点击的事件处理程序等),因为没有捕获异常可能会使该过程失效。

This also means that exception clauses should catch the specific exceptions that you can handle. 这也意味着exception子句应该捕获您可以处理的特定异常。

One approach that I sometimes see is to catch the exception, wrap it in a new exception type and throw that. 我有时看到的一种方法是捕获异常,将其包装在一个新的异常类型中并抛出它。 While that offers you some traceability, I also feel that it removes some options in the exception handling in calling code, such as having specific catch clauses for different scenarios. 虽然这为您提供了一些可跟踪性,但我也觉得它在调用代码时删除了异常处理中的一些选项,例如为不同的场景设置了特定的catch子句。 In these cases you will instead need to have if-clauses inspecting the inner exception with is less readable in my eyes. 在这些情况下,您需要让if-clauses检查内部异常,而在我眼中则不太可读。

B is the library class that I am writing. B是我正在写的库类。

If you have control over B code, B should re throw the exception caught: 如果您可以控制B代码, B应该重新throw捕获的异常:

public class B
{
    public static string B_Method()
    {
        string bstr = String.Empty;

        try
        {
            // Do Something
        }
        catch(Exception e)
        {
            // Do Something (Log the exception details)
            throw;
        }

        return bstr;
    }
}

I want the respective method of the two classes A and B to handle the exception occuring in B_Method in their own different ways. 我希望两个类A和B的相应方法以它们自己的不同方式处理B_Method中发生的异常。

Now, B can log the exception and A and C can handle the rethrown exception is their own way. 现在, B可以记录异常,而AC可以通过自己的方式处理重新抛出的异常。

You can rethrow or just let throw the exception in B_Method and catch it in A and C classes. 您可以重新抛出或只是将异常抛出到B_Method中并在A和C类中捕获它。

public class A
{
  public void A_Method()
  {
   try
   {
     string astr = B.B_Method();
   }
  catch(Exception ex)
  {
    //Handle it in A way
  }

}

public class C
{
 public void C_Method()
  {
   try
   {
     string astr = B.B_Method();
   }
  catch(Exception ex)
  {
    //Handle it in C way
  }
}

I think Design Patterns don't really apply here.. It's just OO programming. 我认为设计模式在这里并不适用..它只是OO编程。

remove the try and catch block from class b 从类b中删除try和catch块

public static string B_Method()throws Exception{
    boolean success=compute();
    if(!success){
       throw new Exception("an error occured");
     }
} 
private static boolean compute(){
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM