我有一个应用程序尝试在其构造函数中加载一些预期的注册表设置.
如果无法加载这些(基本的,不可默认的)注册表设置,BCL中最合适的.NET Exception是多少?
例如:
RegistryKey registryKey = Registry.LocalMachine.OpenSubkey("HKLM\Foo\Bar\Baz"); // registryKey might be null! if (registryKey == null) { // What exception to throw? throw new ???Exception("Could not load settings from HKLM\foo\bar\baz."); }
解决方法
我会和
ArgumentException或
ArgumentOutOfRangeException一起去
throw new ArgumentException("Could not find registry key: " + theKey);
引用MSDN:
The exception that is thrown when one
of the arguments provided to a method
is not valid.
…
IMO写一个正确的异常消息更重要.