0

Following the .NET documentation on the .NET runtime exception events, I am trying to map the ExceptionFlags whose value is UInt16 to an Enum:

[Flags]
public enum ExceptionFlags : UInt16
{
    HasInnerException = 0x01,
    IsNestedException = 0x02,
    IsRethrownException = 0x04,
    IsCorruptedException = 0x08,
    IsCLSComplaint = 0x10
}

But the values that are coming from the event fall outside the values in the doc. For example I am getting 17 and sometimes 16.

What is the logic to map those values into the flags on the enum?

MaYaN
  • 6,683
  • 12
  • 57
  • 109
  • To clarify your question, do you already know how a *flags* enum works in general - i.e. are you familiar with how they use powers of 2? You're just not familiar with the `0x` hexadecimal notation? – Joe Sewell Sep 22 '22 at 19:06
  • I am familiar with how the enum with flags work I just don't know what all the combinations are. – MaYaN Sep 22 '22 at 19:08

1 Answers1

1

Values preceded by 0x are hexadecimal literals. The equivalent using decimal literals would be:

[Flags]
public enum ExceptionFlags : UInt16
{
    HasInnerException = 1,
    IsNestedException = 2,
    IsRethrownException = 4,
    IsCorruptedException = 8,
    IsCLSComplaint = 16
}

As is typical for an enum marked [Flags], each predefined value is a power of 2. They can be combined together with the bitwise OR | to produce values not listed. See this answer for more details.

Your examples:

  • Decimal 16 = Hexadecimal 0x10 = Binary 0001 0000 = IsCLSComplaint. This indicates that the exception is CLS compliant, but it doesn't have an inner exception, and it's not a nested, rethrown, or corrupted exception.

  • Decimal 17 = Hexadecimal 0x11 = Binary 0001 0001 = IsCLSCompliant | HasInnerException. This indicates that the exception is CLS compliant and it has an inner exception, but it's not a nested, rethrown, or corrupted exception.

Joe Sewell
  • 6,067
  • 1
  • 21
  • 34
  • So I will need to consruct all the possible combinations and include them on the Enum before I can map the value, correct? That's going to be a pain! – MaYaN Sep 22 '22 at 19:25
  • No, you don't need to include all the possible combinations, just each of the individual flags, as long as each is given a value equal to a power of 2. See the linked answer for more details on how this works. – Joe Sewell Sep 22 '22 at 19:26
  • Still a bit confused, Given any UInt16, how can I return an ExceptionFlags enum without having to specify the combinations on the enum itself? Let's say I have a method `private bool TryParse(UInt16 value, out ExceptionFlags result)` – MaYaN Sep 22 '22 at 19:38
  • 1
    The possible values of an enum variable in C# are not limited to just those specified - the specified values are just named constants that are there for coding and string formatting convenience. In essence, an `ExceptionFlags` value is already an `UInt16` - you can just cast it `(ExceptionFlags) yourUInt16Value`. – Joe Sewell Sep 22 '22 at 19:59