Home > Blockchain >  How can I convert ExceptionThrown_V1.ExceptionFlags to a valid C# Enum?
How can I convert ExceptionThrown_V1.ExceptionFlags to a valid C# Enum?

Time:09-23

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?

CodePudding user response:

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.

  • Related