I'm having difficulty properly controlling flow in a piece of my code. I have a function GetVoucherNumberAndApplyToDatabase()
which calls a separate function, doPatientEncounterCreate()
. If the inner function throws an exception I want the outer function to stop what it's doing there as it's contingent on the returned value returned.
Here is the code for some clarity:
//outer function
private int GetVoucherNumberAndApplyToDatabase(int Mode)
{
int ProviderID = 0;
int PracticeMRNumber = 0;
int VoucherNumber = 0;
string EncounterD = DateTime.Now.ToString("MM\\/dd\\/yyyy");
try
{
EncounterCreateResults = doPatientEncounterCreate(practiceID, PracticeMRNumber, ProviderID, ddlVisitType.SelectedValue, EncounterD);
/*
if (EncounterCreateResults.ErrorMessage.Length > 0)
{
throw Exception;
}
*/
...
//fails bc doPatientEncounterCreate threw exception
ApplyVoucherNumberToBillingCharges(practiceID, ReasonCodeID);
}
catch (ReasonCodeException ex)
{
pnlError.Visible = true;
lblErrorMessage.Text = ex.Message;
}
catch (Exception ex)
{
pnlError.Visible = true;
lblErrorMessage.Text = "An error occurred attempting to call the API: " + ex.Message;
}
}
//inner function
public static EncounterResults doPatientEncounterCreate(int PracticeID, int PatientID
, int ProviderID, string ReasonCodeID, string EncounterD)
{
try
{
DataTable ReasonCodeDT = getSingleReasonCode(PracticeID, ReasonCodeID);
string strReasonCode = String.Empty;
string strReasonDescription = String.Empty;
string strDuration = String.Empty;
string strActive = String.Empty;
if (ReasonCodeDT.Rows.Count != 1)
{
string strPracticeID = PracticeID.ToString();
string PracticeName = getSingleStringResultFromSQL("SELECT @result ...);
string RecordCount = ReasonCodeDT.Rows.Count.ToString();
throw new ReasonCodeException("Database Returned " + RecordCount + " Records for ReasonCodeID " + ReasonCodeID + " for Practice " + PracticeName + " (" + strPracticeID + ")");
}
else
{
DataRow Row = ReasonCodeDT.Rows[0];
strReasonCode = Row["ReasonCodeID"].ToString();
strReasonDescription = Row["Description"].ToString();
strDuration = Row["Duration"].ToString();
strActive = Row["Active"].ToString().ToLower();
}
string strReasonID = @"""ReasonCode"": {""Code"": """ + strReasonCode + @""", ""Description"":""" + strReasonDescription + @"""" +
@", ""Duration"": " + strDuration + @", ""Active"": " + strActive + @"}, ";
}
/*
catch (ReasonCodeException ex)
{
Results.ErrorMessage = ex.Message;
return Results;
}*/
catch (Exception ex)
{
ErrorLog.LogError(ex, "doPatientEncounterCreate Failed.", Convert.ToInt32(au.UserID), "");
Results.ErrorMessage = ex.ToString();
return Results;
}
}
//Custom Exception Class used in doPatientEncounterCreate
class ReasonCodeException : Exception
{
public ReasonCodeException()
{
}
public ReasonCodeException(string Message):
base ()
{
ErrorLog.LogError(Message);
}
}
I've taken the advice of the second answer to this qustion: Exit all functions from code in inner function
However, even though I've moved my ReasonCodeException
catch block to the outer function, when the code is run, a generic exception is thrown in the for the inner function doPatientEncounterCreate() and then the code continues in the oter function until an exception is throw because it is missing the value returned from the inner function.
Also, I can't follow the advice of the accepted answer to the above question because the inner function isn't a void function, I need the value returned.
Any help you can provide is greatly appreciated.