Justin Dearing
Justin Dearing

Reputation: 14948

What is the proper exception to throw if an ODBC driver cannot be found

I have the following code that searches for installed Microsoft Access drivers:

var odbcRegKey = Registry.LocalMachine.OpenSubKey(
    "SOFTWARE\\ODBC\\ODBCINST.INI\\ODBC Drivers", false);
var drivers = new List<string>(odbcRegKey.GetSubKeyNames());
if (drivers.Contains("Microsoft Access Driver (*.mdb, *.accdb)"))
{
    MicrosoftAccessProvider = "Microsoft Access Driver (*.mdb, *.accdb)";
}
else if (drivers.Contains("Microsoft Access Driver (*.mdb)"))
{
    MicrosoftAccessProvider = "Microsoft Access Driver (*.mdb)";
}
else
{
    //TODO: Throw some kind of excception
}

What is the proper exception to throw if it cannot find the ODBC driver? There are no public constructors for OdbcException()

Upvotes: 2

Views: 458

Answers (3)

Mark Brackett
Mark Brackett

Reputation: 85665

When in doubt, InvalidOperationException is my go-to choice. If it's something configurable (perhaps being able to use something other than Access), consider ConfigurationException as well.

Upvotes: 2

Hans Passant
Hans Passant

Reputation: 941942

It is pretty rare for it to make sense to try to continue running a program in cases like this. Whatever code catches this exception won't know how to install the provider either. MessageBox.Show() and Environment.Exit() is then appropriate. Only ever consider throwing an exception if the program can limp along without a dbase.

Upvotes: 1

Davide Piras
Davide Piras

Reputation: 44605

I would actually throw like this:

throw new NotSupportedException("ODBC Driver not found!");

Upvotes: 0

Related Questions