futurenext110
futurenext110

Reputation: 2109

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:

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. Now there are lets say two other classes say A and 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.

I looked for framework design pattern, but felt that was not useful.

Upvotes: 3

Views: 4272

Answers (4)

Azodious
Azodious

Reputation: 13872

B is the library class that I am writing.

If you have control over B code, B should rethrow the exception caught:

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.

Now, B can log the exception and A and C can handle the rethrown exception is their own way.

Upvotes: 1

elex
elex

Reputation: 76

remove the try and catch block from class b

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

Upvotes: 0

margabit
margabit

Reputation: 2954

You can rethrow or just let throw the exception in B_Method and catch it in A and C classes.

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.

Upvotes: 0

Fredrik Mörk
Fredrik Mörk

Reputation: 158309

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.

This also means that exception clauses should catch the specific exceptions that you can handle.

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. In these cases you will instead need to have if-clauses inspecting the inner exception with is less readable in my eyes.

Upvotes: 9

Related Questions