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

◇◆丶佛笑我妖孽 提交于 2019-12-11 02:32:33

问题


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()


回答1:


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.




回答2:


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.




回答3:


I would actually throw like this:

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


来源:https://stackoverflow.com/questions/7221703/what-is-the-proper-exception-to-throw-if-an-odbc-driver-cannot-be-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!