In a “using” block is an OracleConnection closed if System.Environment.Exit(0) is issued in the catch statement?
Example:
OracleConnection oracleConnection = getOracleConnection();
using (oracleConnection)
{
try
{
oracleConnection.Open();
OracleCommand cmd = getApplicantsCmd(oracleConnection);
OracleDataReader rdr = cmd.ExecuteReader();
List<Applicant> applicants = new List<Applicant>();
while (rdr.Read())
{
Applicant applicant = new Applicant();
applicant.email = (string)rdr["GOREMAL_EMAIL_ADDRESS"];
applicants.Add(applicant);
}
return applicants;
}
catch (Exception ex)
{
Console.WriteLine("Failure getting applicant records: " + ex.Message);
System.Environment.Exit(0);
return null;
}
}
I want execution to stop if an exception is thrown while looking up the records.
Is there a better way to handle this?
There will be no call to
oracleConnection'sDispose()method after the call ofSystem.Environment.Exit, even though the variable is wrapped inusing. If you would like to make sure that the connection is disposed, either put theusinginsidetry/catch, or do not callSystem.Environment.Exitinsideusing. You can still exit if you set a flag, and act upon it after theusingstatement: