c# – 无法加载预期的注册表设置时,最合适的.NET异常是什么?

前端之家收集整理的这篇文章主要介绍了c# – 无法加载预期的注册表设置时,最合适的.NET异常是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个应用程序尝试在其构造函数中加载一些预期的注册表设置.

如果无法加载这些(基本的,不可默认的)注册表设置,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.");
 }

解决方法

我会和 ArgumentExceptionArgumentOutOfRangeException一起去
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写一个正确的异常消息更重要.

原文链接:https://www.f2er.com/csharp/94323.html

猜你在找的C#相关文章