我正在尝试处理 NullReference 异常,但我很困惑如何处理它。这是引发 NullReference 异常的示例代码:
private Customer GetCustomer(string unformatedTaxId)
{
return loan.GetCustomerByTaxId(new TaxId(unformatedTaxId));
}
现在我用下面的方法处理这个
public void ProcessApplicantAddress(ApplicantAddress line)
{
try
{
Customer customer = GetCustomer(line.TaxId);
//if (customer == null)
//{
// eventListener.HandleEvent(Severity.Informational, line.GetType().Name, String.Format("Could not find the customer corresponding to the taxId '{0}' Applicant address will not be imported.", new TaxId(line.TaxId).Masked));
// return;
//}
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(NullReferenceException e)
{
//eventListener.HandleEvent(Severity.Informational, line.GetType().Name, String.Format("Could not find the customer corresponding to the taxId '{0}' Applicant address will not be imported.", new TaxId(line.TaxId).Masked));
eventListener.HandleEvent(Severity.Informational, line.GetType().Name, e.Message);
}
}
我之前写的例子是 if(customer == null) 现在我应该去掉那个代码,以便我可以在 catch 块中处理它。
请帮助我如何抛出该异常。