I have some code where I want to throw two exceptions; however, the exception are basically the same but with different values. I would like to know an elegant generic way to determine which of these errors occurred.
I know I can do two try catches or that I can set a Boolean to determine the success of query. I am also aware that this can be done in 1 query; however, I need to be able to determine if the company key was wrong or if PA id was wrong. I also know that I can create my own exception and add an additional field to it. Unfortunately, I do not believe that any of these are the optimal solution and this has been bothering me for quite some time.
Any information as to best practice would be appreciated.
using (var ora = new OracleConnection(Data.ConnectionString))
{
String sqlGetCompanyId = "SELECT COMPANY_ID FROM companies WHERE key = :key";
String sqlValidateDelete = "select * from pa where PA_ID = :paid AND COMPANY_ID = :cid";
ora.Open();
int CompanyId = 0;
using (var Command = ora.CreateCommand())
{
Command.CommandText = sqlGetCompanyId;
Command.Parameters.Add(":key", OracleDbType.Varchar2).Value = cKey;
using (var reader = Command.ExecuteReader(CommandBehavior.SingleRow))
{
if (reader.Read())
CompanyId = unchecked((int)((long)reader["COMPANY_ID"]));
else
throw new ArgumentException("Invalid Company Key");
}
}
using (var Command = ora.CreateCommand())
{
Command.CommandText = sqlValidateDelete;
Command.Parameters.Add(":cid", OracleDbType.Int32).Value = CompanyId;
Command.Parameters.Add(":paid", OracleDbType.Int32).Value = PAID;
using (var reader = Command.ExecuteReader(CommandBehavior.SingleRow))
{
if (!reader.Read())
throw new ArgumentException("Price Agreement Id for this company does not exist");
rv = unchecked((int)((long)reader["ROW_VERSION"]));
}
}
}