Deriving an error code from an exception? 
Author Message
 Deriving an error code from an exception?

Hi all,

I'm looking to derive an error code from an exception. After investigating a
bit, it seems as though an Exception has a protected HResult property.
According to MSDN:

"HRESULT is a 32-bit value, divided into three different fields: a severity
code, a facility code, and an error code. The severity code indicates
whether the return value represents information, warning, or error. The
facility code identifies the area of the system responsible for the error.
The error code is a unique number that is assigned to represent the
exception. Each exception is mapped to a distinct HRESULT. "

So there is an error code inside the HRESULT.

In the platform SDK documentation, I found this information on HRESULT:

"On 32-bit platforms, the HRESULT data type is the same as the SCODE data
type. On 16-bit platforms, an SCODE value is used to generate an HRESULT
value.
An HRESULT value is made up of the following fields:

  a.. A 1-bit code indicating severity, where zero represents success and 1
represents failure.
  b.. A 4-bit reserved value.
  c.. An 11-bit code indicating responsibility for the error or warning,
also known as a facility code.
  d.. A 16-bit code describing the error or warning."
So, anyone know how I can get out the error code from a HRESULT that is
stored in the protected property Hresult of the Exception class?

Thanks in advance,
  Mark.



Sat, 23 Oct 2004 18:17:37 GMT  
 Deriving an error code from an exception?
The best approach I can come up with is somewhat archaic,
but since C# doesn't support bitwise operations directly,
well, here goes:

Take HResult (which, since its protected, means you'll
have to derive a new class from Exception and expose it)
and then map it to a BitArray.

Loop through the last 16 bits of the BitArray, and
calculate the number of each "on" bit based on the proper
exponent of 2.

Add them up.

Here is my over simplified derived Exception class:
internal class myException : Exception
{
        public myException(string text) : base(text)
        {
        }

        public int myHR
        {
                get
                {
                        return base.HResult;
                }
        }

Quote:
}

and here is code I used to get the error number:
try
{
        throw new myException("I hate you");
Quote:
}

catch(myException ex)
{
        int[]  myHR  = new int[1] { ex.myHR };
        BitArray ba = new BitArray( myHR );

        long ec = 0;
        for(int i = 16; i <32; i++)
        {
                if(ba[i])
                {
                        ec += (long)Math.Pow(2,(32 - i));
                }
        }
        MessageBox.Show(ec.ToString());
        return;

Quote:
}

crude but I think its correct.

Quote:
>-----Original Message-----
>Hi all,

>I'm looking to derive an error code from an exception.

After investigating a
Quote:
>bit, it seems as though an Exception has a protected
HResult property.
>According to MSDN:

>"HRESULT is a 32-bit value, divided into three different
fields: a severity
>code, a facility code, and an error code. The severity
code indicates
>whether the return value represents information, warning,
or error. The
>facility code identifies the area of the system

responsible for the error.
Quote:
>The error code is a unique number that is assigned to
represent the
>exception. Each exception is mapped to a distinct
HRESULT. "

>So there is an error code inside the HRESULT.

>In the platform SDK documentation, I found this

information on HRESULT:
Quote:

>"On 32-bit platforms, the HRESULT data type is the same
as the SCODE data
>type. On 16-bit platforms, an SCODE value is used to
generate an HRESULT
>value.
>An HRESULT value is made up of the following fields:

>  a.. A 1-bit code indicating severity, where zero

represents success and 1
Quote:
>represents failure.
>  b.. A 4-bit reserved value.
>  c.. An 11-bit code indicating responsibility for the
error or warning,
>also known as a facility code.
>  d.. A 16-bit code describing the error or warning."
>So, anyone know how I can get out the error code from a
HRESULT that is
>stored in the protected property Hresult of the Exception
class?

>Thanks in advance,
>  Mark.

>.



Sat, 23 Oct 2004 20:48:14 GMT  
 Deriving an error code from an exception?
Mark,

    This is not what you should be doing to get error information.  There
doesn't always have to be a mapping between an exception and an HRESULT.
Even if there was, there are not always mappings between HRESULTS and error
codes on the Win32 platform.

    You should be making determinations based on the type of the exception
thrown, and the instance information for an exception of that type.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Hi all,

> I'm looking to derive an error code from an exception. After investigating
a
> bit, it seems as though an Exception has a protected HResult property.
> According to MSDN:

> "HRESULT is a 32-bit value, divided into three different fields: a
severity
> code, a facility code, and an error code. The severity code indicates
> whether the return value represents information, warning, or error. The
> facility code identifies the area of the system responsible for the error.
> The error code is a unique number that is assigned to represent the
> exception. Each exception is mapped to a distinct HRESULT. "

> So there is an error code inside the HRESULT.

> In the platform SDK documentation, I found this information on HRESULT:

> "On 32-bit platforms, the HRESULT data type is the same as the SCODE data
> type. On 16-bit platforms, an SCODE value is used to generate an HRESULT
> value.
> An HRESULT value is made up of the following fields:

>   a.. A 1-bit code indicating severity, where zero represents success and
1
> represents failure.
>   b.. A 4-bit reserved value.
>   c.. An 11-bit code indicating responsibility for the error or warning,
> also known as a facility code.
>   d.. A 16-bit code describing the error or warning."
> So, anyone know how I can get out the error code from a HRESULT that is
> stored in the protected property Hresult of the Exception class?

> Thanks in advance,
>   Mark.



Sat, 23 Oct 2004 20:51:04 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. cvpack INTERNAL ERROR, exception code = 0xc0000005

2. Exceptions and Error Codes

3. Exporting class derived from std::exception hierarchy

4. Exporting a class derived from exception?

5. Exception's in derived CFileDialog

6. W4 warning - throwing CObject derived class as exception

7. Addendum: COleServerDoc-derived class exception failure

8. COleServerDoc-derived class exception failure

9. Internal Compiler Error while deriving C# class from a MC++ class implementing a C# interface

10. error handling and exit of ServiceBase-derived app

11. VC++ error: Derived class Virtual member functions.

12. derived class error

 

 
Powered by phpBB® Forum Software