我试图找出使用try-catch语句处理C#中的异常的最佳途径,这有点令人沮丧。我想拥有代码,所以如果尝试打开注册表项时抛出异常,那么它将转到下一个注册表项。我能想到的唯一方法是将每个函数调用封装在try-catch语句中,但对我而言,这似乎还有很长的路要走。
ScanSubKey
函数是否可以用来检查异常? 没有try-catch语句
ScanSubKey(Registry.ClassesRoot);
ScanSubKey(Registry.LocalMachine.OpenSubKey("SOFTWARE"));
ScanSubKey(Registry.CurrentUser.OpenSubKey("SOFTWARE"));
if (Is64BitOS)
{
ScanSubKey(Registry.ClassesRoot.OpenSubKey("Wow6432Node"));
ScanSubKey(Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node"));
ScanSubKey(Registry.CurrentUser.OpenSubKey("SOFTWARE\\Wow6432Node"));
}
示例
try {
ScanSubKey(Registry.ClassesRoot);
}
catch (Exception ex) {
Debug.WriteLine("The following error occurred opening a registry key: " + ex.Message);
}
try {
ScanSubKey(Registry.LocalMachine.OpenSubKey("SOFTWARE"));
}
catch (Exception ex) {
Debug.WriteLine("The following error occurred opening a registry key: " + ex.Message);
}
try {
ScanSubKey(Registry.CurrentUser.OpenSubKey("SOFTWARE"));
}
catch (Exception ex) {
Debug.WriteLine("The following error occurred opening a registry key: " + ex.Message);
}
if (Is64BitOS)
{
try {
ScanSubKey(Registry.ClassesRoot.OpenSubKey("Wow6432Node"));
}
catch (Exception ex) {
Debug.WriteLine("The following error occurred opening a registry key: " + ex.Message);
}
try {
ScanSubKey(Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node"));
}
catch (Exception ex) {
Debug.WriteLine("The following error occurred opening a registry key: " + ex.Message);
}
try {
ScanSubKey(Registry.CurrentUser.OpenSubKey("SOFTWARE\\Wow6432Node"));
}
catch (Exception ex) {
Debug.WriteLine("The following error occurred opening a registry key: " + ex.Message);
}
}
答案 0 :(得分:1)
也许这会是一个更好的方法:
public bool TryOpenRegKey(RegistryKey regKey, string subKey = default(string))
{
try
{
if (string.IsNullOrWhiteSpace(subKey))
ScanSubKey(regKey);
else
ScanSubKey(regKey.OpenSubKey(subKey));
return true;
}
catch(Exception ex)
{
Debug.WriteLine("The following error occurred opening a registry key: " + ex.Message);
return false;
}
}
然后:
TryOpenRegKey(Registry.ClassesRoot);
TryOpenRegKey(Registry.LocalMachine, "SOFTWARE");
TryOpenRegKey(Registry.CurrentUser, "SOFTWARE");
if (Is64BitOS)
{
TryOpenRegKey(Registry.ClassesRoot, "Wow6432Node");
TryOpenRegKey(Registry.LocalMachine, "SOFTWARE\\Wow6432Node");
TryOpenRegKey(Registry.CurrentUser, "SOFTWARE\\Wow6432Node");
}
答案 1 :(得分:1)
创建新方法TryScanSubKey
。然后你的代码再次看起来很干净:
TryScanSubKey(Registry.ClassesRoot);
TryScanSubKey(Registry.LocalMachine.OpenSubKey("SOFTWARE"));
TryScanSubKey(Registry.CurrentUser.OpenSubKey("SOFTWARE"));
if (Is64BitOS)
{
TryScanSubKey(Registry.ClassesRoot.OpenSubKey("Wow6432Node"));
TryScanSubKey(Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node"));
TryScanSubKey(Registry.CurrentUser.OpenSubKey("SOFTWARE\\Wow6432Node"));
}
TryScanSubKey
的实施应该是显而易见的,但是,为了完整起见,这里是:
void TryScanSubKey(RegistryKey key)
{
try
{
ScanSubKey(key);
}
catch (Exception ex)
{
Debug.WriteLine("The following error occurred opening a registry key: " + ex.Message);
}
}
请注意,一般,吞下错误是不好的做法,吞下所有错误的情况更糟。因此,请考虑(至少)用更具体的内容替换catch (Exception ex)
,例如catch (TheSpecificExceptionThrownByScanSubKey ex)
。
答案 2 :(得分:0)
我称之为“执行者”:
public static void Execute(Action action)
{
try
{
action();
}
catch (Exception ex)
{
}
}
然后使用它:
Execute(() => ScanSubKey(Registry.ClassesRoot));
答案 3 :(得分:0)
您是否考虑在ScanSubKey函数本身内部放置try / catch并在发生错误时返回?