我有两种方法,其中方法 1 抛出异常。方法 2 调用方法 1,如果它抛出异常应该在方法 2 中处理。
这是我的方法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));
}
现在在以下方法中,我正在调用方法 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);
}
我的问题是我得到一个未处理的异常,但我应该在方法 2 中捕获它。请帮助我。
我的 NotFoundException 类
// 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)
{
}
}