JJ.
JJ.

Reputation: 9970

How should I handle an exception between two classes?

See questions in the comments of the code.

I have two classes.

Here's the main class (class 1):

//this class contains controls on the form.
Class MyApp
{
    private void btnProcessImages_Click(object sender, EventArgs e)
    {
        /* if an error occurs in the method below, I want to: 
               1. Show the error message, and
               2. Based on fail or success do different actions.
        */
        Calculate.DevideNumbers(2, 0);
    }

    /* 
       if the above is successful, I want to do 1 thing, 
       if not, i want to do something else (with controls on THIS form).
    */ 

}

And here's the second class:

Class Calculate
{
     public double void DivideNumbers(int num1, int num2)
     {
           double result = 0.00;

           try
           {
                result = num1/num2;
                return result;
           }
           catch (Exception)
           {
                throw;
           }  
     }
}

My question is: What is the best way for DivideNumbers() to report an error back to the calling method?

The caller needs to know if there was an error and what the error message was. How would I go about sending the calling method these two pieces of information?

Upvotes: 0

Views: 1287

Answers (5)

Omar
Omar

Reputation: 16623

You can do:

class MyApp : Form
{
    //...

    bool erroroccurs = false;
    private void btnProcessImages_Click(object sender, EventArgs e)
    {
          try
          {
              Calculate.DevideNumbers(2, 0);
          }
          catch(Exception ex)
          {
              MessageBox.Show(ex.Message);
              erroroccurs = true;
          }
    }
}

Upvotes: 1

Jeremy Wiggins
Jeremy Wiggins

Reputation: 7299

Here's my recommendation. Note a few things I changed that aren't extremely obvious.

  • DivideNumbers returns a double (you had it returning a value, but the method was marked as void.
  • I cast one int to a double in DivideNumbers. An int divided by an int is always an int. This can produce unexpected results, unless you want/expect implicit rounding.
  • Exception handling is done in a different class, since it seems that's where you want to handle it. I.E. what's the point of catching an exception if your only intent is to re-throw it? Just let it bubble up.

...

Class MyApp
{
    private void btnProcessImages_Click(object sender, EventArgs e)
    {
        try 
        {
            double result = Calculate.DevideNumbers(2, 0);
            HandleSuccess(result);
        }
        catch (Exception ex)
        {
            HandleError(ex.Message);
        }
    }

    private void HandleSuccess(double result)
    {
        // Do whatever you do when no errors occur
    }

    private void HandleError(string errorMessage)
    {
        // Do whatever you do when an error occurs... log the exception, etc.
    }
}

...

Class Calculate
{
    public static double DivideNumbers(int num1, int num2)
    {
        result = (double)num1/num2;
        return result;

        // There's no need to catch an exception here if you're ONLY going to re-throw it.
    }        
}

Upvotes: 0

Chris Kooken
Chris Kooken

Reputation: 33938

Remove the try catch in DivideNumbers and let the exception bubble up.

Then Wrap the call to Calculate.DevideNumbers(2, 0); in a try catch block.

//this class contains controls on the form.
Class MyApp
{
    private void btnProcessImages_Click(object sender, EventArgs e)
    {
      try
      {
          Calculate.DevideNumbers(2, 0);
      }
      catch (Exception e)
      {
           DoStuff();
           Return();
      }
    }
}

Upvotes: 3

Claudio Redi
Claudio Redi

Reputation: 68440

First, you should not add a try catch block on DevideNumbers since, according the code you provided, you're doing nothing with the exception. You only need to catch exceptions in case you plan to do something with it (log the error for instance)

The exception will bubble up anyway so you don't need that try catch block

On the caller method you could catch any exception when calling DevideNumbers and do your magic.

Upvotes: 0

Erwin
Erwin

Reputation: 4817

Remove the try catch in your DevideNumbers method, I has no use there because you don't to anything in the catch. Is better to place your try catch int the btnProcessImages_Click method and there you can add the error handling.

Upvotes: 1

Related Questions