Jason P
Jason P

Reputation:

How to handle multiple errors in C#?

I have some code that reads 10 registry keys, sometimes the values are not present sometimes the keys are not present, sometimes the value isn't boolean etc etc. How should I add error handling to this, currently it is placed in one big try{} catch{} but if the second value I read fails then the rest are not read as the program jumps to catch{}, I could add a try{} catch{} for each but I'm guessing there is a better way. How would you handle this? I'm asking as I regularly come across similar problems and my own solution is to add a try{} catch{}.

Thanks for the help.

Upvotes: 4

Views: 1035

Answers (8)

Scott Dorman
Scott Dorman

Reputation: 42526

@Marc's answer is the best, but if you absolutely must have a single excpetion that contains the collection of registry keys that had errors you should look at using the Data property of the exception. From the MSDN documentation on this property,

Use the System.Collections.IDictionary object returned by the Data property to store and retrieve supplementary information relevant to the exception. The information is in the form of an arbitrary number of user-defined key/value pairs. The key component of each key/value pair is typically an identifying string, whereas the value component of the pair can be any type of object.

Upvotes: 0

MusiGenesis
MusiGenesis

Reputation: 75386

How are you reading the registry values? The Registry class (Microsoft.Win32.Registry) allows you to read a registry value and return a default value that you specify if the value/name pair does not exist, like this:

object o = Microsoft.Win32.Registry.GetValue(
    @"HKEY_CURRENT_USER\Software\Microsoft\Calc", "layout", "");

The last parameter here is the specified default to be returned if the value name is not found. I made it a blank string, but you can change this to whatever you like.

Upvotes: 1

FlySwat
FlySwat

Reputation: 175733

Dictionary<String,String> regKeys = new Dictionary<String,String>()
{
    { "Key1", String.Empty},
    { "Key2", String.Empty},
    { "Key3", String.Empty}
};

for (int i = 0; i < regKeys.Length; i++)
{
   try
   {
       regKeys[i].Value = ReadFromRegistry(regKeys[i].Key);
   }
   catch (Exception ex)
   {
      Console.WriteLine("Unable to read Key: " + regKeys[i].Key 
         + " Exception: " + ex.Message);
   } 
}

Upvotes: 1

Pure.Krome
Pure.Krome

Reputation: 87047

EDIT: changed it all around. :P I suggested a struct or class (previously) but now i'm changing that to a simple string collection.

some pseduo code off the top of my head....

public IEnumerable<string> ReadRegistryKeys()
{
    IEnumerable<string> resultList = new List<string>();
    if (string.IsNullOrEmpty(read_in_key_#1())
    {

        resultList.Add("Failed to load key 'blah'..");
    }

    if (.... read in the next key .. etc.... ) ...

    return resultList == null || resultList.Count <= 0 ? null : resultList;
}

You can use a StringCollection (System.Collections.Specialized?) also, if u feel like it.

Upvotes: 0

JSBձոգչ
JSBձոգչ

Reputation: 41388

This depends on the severity of the errors. Is it meaningful and useful for the program to continue if some of the keys it looks up are missing or have the wrong type? Are some of the keys more important than others?

I would suggest the following:

  • Find all of the keys that you have to have, and put them in a single try{}, with the catch{} that reports the fatal error and initiates cleanup. Execute this block first.

  • Find all of the optional keys and put each of them in their own try{} block, which allows you to recover and go on to the other keys. To make this simpler, you can add a wrapper method that has the necessary try/catch block and error checking, and which takes the key name as a parameter.

Upvotes: 0

Paul Sonier
Paul Sonier

Reputation: 39510

Essentially, yes, you want to define the error-handling on each individual element, not define the error handling on the set of elements. That is, if you want each error to be caught but not cause the process to abort, you should perform error handling on each individual element, not on the group as a whole.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1064014

First, swallowing exceptions is generally a bad idea - could you not write a method that checks the keys etc for existance, and returns the value if one?

If that absolutely, positively isn't possible, you can refactor the code into multiple calls to a single method that (for each) does a try/catch (swallow):

SomeReturnType foo = HackyMethod(first path);
SomeReturnType bar = HackyMethod(sedond path);

SomeReturnType HackyMethod(string path)
{
    try {} catch {} etc
}

Upvotes: 2

Ed Marty
Ed Marty

Reputation: 39700

Refactor the code that reads the values into its own function that handles the errors how you want them to be handled.

Upvotes: 0

Related Questions