Enums & Flags

  • Thread starter Thread starter Rajesh Abraham
  • Start date Start date
R

Rajesh Abraham

I am running loop with calls different functions such as
SetHomeDrive, AddUserToGroup etc. Now if any of the
functions fails, I am just catching and throwing a new
error like throw new Exception("Failed to Set Home Drive")
etc.

All these errors which are propelled back to my loop are
then logged to file. Even if an error occours in one ore
more function, the loop continues execution so I deally I
would like to have unique flag for each errror, which can
be OR ed each time an error occurs and finally at the end
of my loop find from the error flag what all errors
occured. How can I acheive this.

Thanks,

Rajesh Abraham Chacko
 
Rajesh,
Generally you should not be raising System.Exception directly, you should
raise a different existing exception that is appropriate for the error or
you should define one of your own.

In this case I would define a new Exception class that has an enum property
for your unique flag. Define this unique flag as an Enum that has the Flags
attribute. Remember to set the values of the Enum to powers of 2: 1, 2, 4,
8, 16, 32, 64, 128... This exception class should derive from
System.ApplicationException. At the very least I would give it a constructor
that accepted this Enum, and a text string. In the main loop I would catch
this type of Exception, Or-ing the value with an accumulator.

See "Error Raising and Handling Guidelines" in the Design Guidelines for
Class Library Developers.

http://msdn.microsoft.com/library/d.../html/cpconerrorraisinghandlingguidelines.asp

Hope this helps
Jay
 
Back
Top