1

I have two methods in which method 1 throw's an exception. Method 2 calls method 1 and if it throws exception it should be handled in method 2.

Here is my method 1

 private Customer GetCustomer(string unformatedTaxId)
        {
            if (loan.GetCustomerByTaxId(new TaxId(unformatedTaxId)) == null)
            {
                throw new NotFoundException("Could not find the customer corresponding to the taxId '{0}' Applicant address will not be imported.", new TaxId(unformatedTaxId));

            }

            return loan.GetCustomerByTaxId(new TaxId(unformatedTaxId));
         }

Now in the following method i am calling method 1

public void ProcessApplicantAddress(ApplicantAddress line)
        {
            try
            {
                Customer customer = GetCustomer(line.TaxId);


                Address address = new Address();
                address.AddressLine1 = line.StreetAddress;
                address.City = line.City;
                address.State = State.TryFindById<State>(line.State);
                address.Zip = ZipPlusFour(line.Zip, line.ZipCodePlusFour);
              }
              catch(NotFoundException e)
            {
                    eventListener.HandleEvent(Severity.Informational, line.GetType().Name, e.Message);
            }

My problem is i am getting an unhandled exception, but i should catch it in the method 2. Please help me from this.

My NotFoundException Class

//  class NotFoundException


  public class NotFoundException : Exception
    {



    public NotFoundException() : base() 
    { 
    }


    public NotFoundException(string message): base(message) 
    { 
    }


    public NotFoundException(string format, params object[] args): base(string.Format(format, args)) 
    { 
    }

    public NotFoundException(string message, Exception innerException) : base(message, innerException) 
    { 
    }


    public NotFoundException(string format, Exception innerException, params object[] args) : base(string.Format(format, args), innerException)
    { 
    }
}
user2619542
  • 223
  • 1
  • 5
  • 11
  • 2
    This should work. What is the detail of the unhandled exception? Maybe it's not the exception you think it is. – John Saunders Sep 05 '13 at 16:43
  • Your NotFoundException has a (string, TaxID) constructor? Why that over just a string? – User Sep 05 '13 at 16:46
  • As a debugging tip: press Ctlr + Alt + E, which brings up the Exceptions dialog. Select `Thrown` for `Common Language Runtime Exceptions`. – gunr2171 Sep 05 '13 at 16:54
  • can you please post the code for the class NotFoundException specially the constructor method – Mauricio Gracia Gutierrez Sep 05 '13 at 17:12
  • When the indenting is messed up like this, we almost always look at a code snippet that doesn't match the real code. And cannot show the cause of the problem. – Hans Passant Sep 05 '13 at 17:17

2 Answers2

0

Try-Catch blocks are relatively expensive operations, and they can make debugging difficult. I highly recommend you avoid using an error handling pattern with try-catch. Instead, you could just skip the method that throws and check for null:

public void ProcessApplicantAddress(ApplicantAddress line)
{

    var customer = loan.GetCustomerByTaxId(new TaxId(line.TaxId));

    if (customer == null)
    {
        eventListener.HandleEvent(Severity.Informational, "ApplicantAddress", String.Format(""Could not find the customer corresponding to the taxId '{0}' Applicant address will not be imported."", line.TaxId));
    }
    else
    {
        var address = new Address();
        address.AddressLine1 = line.StreetAddress;
        address.City = line.City;
        address.State = State.TryFindById<State>(line.State);
        address.Zip = ZipPlusFour(line.Zip, line.ZipCodePlusFour);
        //do whatever else you need to do with address here.
    }
}

Your originally posted code should work, but I am guessing that GetCustomerByTaxId is throwing an exception of its own when the customer isn't found. You might consider stepping through that method and making sure it is actually going to return null when no customer is found.

User
  • 1,118
  • 7
  • 23
  • Yea this is my previous version and it is working fine. But my manager told me to get rid of that and throw an exception in GetCustomer() method and handle it in ProcessApplicantAddress() method using try catch blocks. Any clue to solve this issue? – user2619542 Sep 05 '13 at 17:23
  • To be honest, if you are being directed to do something a certain way, I'd ask your manager to explain in detail the pattern he's looking for, and why. The only other way you'll be able to get help from Stack Overflow is if you post the details of the Unhandled Exception. – User Sep 05 '13 at 17:44
  • Like what kind of details? Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. – user2619542 Sep 05 '13 at 17:49
0

Your throw is never triggered because the functions executed within if statement raised another exception, so the catch isn't trigger because it's another type of exception.

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57