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
's Dispose()
method after the call of System.Environment.Exit
, even though the variable is wrapped in using
. If you would like to make sure that the connection is disposed, either put the using
inside try
/catch
, or do not call System.Environment.Exit
inside using
. You can still exit if you set a flag, and act upon it after the using
statement:
var mustExit = false;
using (var oracleConnection = getOracleConnection()) {
try {
...
} catch {
Console.WriteLine("Failure getting applicant records: " + ex.Message);
mustExit = true;
}
}
if (mustExit) {
System.Environment.Exit(0);
}