Reputation: 5076
I am trying to return the value false
if an exception is caught in a method, but instead, it seems to return an exception rather than the bool.
I use this to call the first method:
try
{
bool wasImported = WriteAddress(theCompany.CompanyId, company);
if(wasImported == false)
{
///some logic here
}
}
catch(Exception ex)
{
string msg = ex.Message;
}
Here is the WriteAddress
method:
private bool WriteAddress(Guid companyId, ModuleView.CompanyBO company)
{
try
{
if (company.First_Address != "" || company.First_Address2 != "" || company.First_City != "" || company.First_State != "" || company.First_Zip != "")
{
bool isDate = false;
DateTime thisDate;
ModuleView.AddressBO theAddress = new ModuleView.AddressBO();
theAddress.CompanyId = companyId;
// theAddress.Address = Conversions.Truncate(company.First_Address,200);
theAddress.Address = company.First_Address;
theAddress.Address2 = Conversions.Truncate(company.First_Address2,200);
theAddress.City = Conversions.Truncate(company.First_City,50);
theAddress.State = Conversions.Truncate(company.First_State,100);
theAddress.Zip = Conversions.Truncate(company.First_Zip,50);
theAddress.Country = Conversions.Truncate(company.First_Country,150);
theAddress.Phone = Conversions.Truncate(company.First_Phone,30);
theAddress.Fax = Conversions.Truncate(company.First_Fax,30);
theAddress.AddressId = CompanyBA.Address_Insert(DB_Context, theAddress);
if(theAddress.AddressId == null)
{
return false;
}
}
else
{
return false;
}
return true;
}
catch(Exception ex)
{
return false;
}
}
Here is the Address_Insert
method:
public static void Address_Insert(COG_ContextDB context, COGS.DomainDataModel.Address item)
{
context.Addresses.Add(item);
context.SaveChanges();
}
I am purposely not truncating the Address field in order to produce an error when the method CompanyBA.Address_Insert(DB_Context, theAddress);
is called. That method returns an error which triggers the catch
block. The catch
block has return false
, but instead of returning false
, and executing the if(wasImported == false)
in the calling method, it returns an error and the first catch block executes. Is there anyway to get the WriteAddress
method to return false
instead of executing the catch
block in the calling method? I hope I am explaining this correctly.
Any assistance is greatly appreciated.
Upvotes: 2
Views: 333
Reputation: 660417
I am trying to return the value false if an exception is caught in a method, but instead, it seems to return an Exception rather than the bool.
That is very unusual, but fortunately you gave the clue in a comment:
"Thread was being aborted" I believe is the message.
If you could catch a thread abort exception and turn it into a return then the thread would never abort! A thread abort exception can be caught but it is automatically re-thrown so that the thread does in fact abort. (Aside: there are ways to prevent or delay an aborted thread from aborting; you must not rely on thread aborting to abort a thread in a timely manner for program correctness.)
That said: Aborting a thread is a worst practice in C# and you should not do so. Since you will now never abort a thread, you will never need to worry about catching a thread abort exception and have to deal with its unusual behaviour.
Upvotes: 5