加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:53:24 所属栏目:百科 来源:网络整理
导读:我有一个应用程序尝试在其构造函数中加载一些预期的注册表设置. 如果无法加载这些(基本的,不可默认的)注册表设置,BCL中最合适的.NET Exception是多少? 例如: RegistryKey registryKey = Registry.LocalMachine.OpenSubkey("HKLMFooBarBaz"); // registr
我有一个应用程序尝试在其构造函数中加载一些预期的注册表设置.

如果无法加载这些(基本的,不可默认的)注册表设置,BCL中最合适的.NET Exception是多少?

例如:

RegistryKey registryKey = Registry.LocalMachine.OpenSubkey("HKLMFooBarBaz");

 // registryKey might be null!
 if (registryKey == null)
 {
     // What exception to throw?
     throw new ???Exception("Could not load settings from HKLMfoobarbaz.");
 }

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读