471,317 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Exception - Error Code

When an exception is generated is there not suppose to be a property which
contains the error code? I'm not seeing this property. Does it not show an
error code if the exception comes from a COM component?

Thanks in advance for your assitance.
Jan 25 '06 #1
2 7057
Jim,

No, there isn't a property for the error code, and in reality, you
shouldn't be using such a mechanism, unless the exception represents a fault
in another domain (as in unmanaged code, or COM). In those cases, you will
have a property which will expose that error code. For example,
SocketExceptions return an error code, as do Win32Exceptions and
COMExceptions.

While it is true that ALL exceptions have a protected property which
exposes an HRESULT, this is used for interop situations. Generally though,
you shouldn't use that in code.

You should depend on the type itself to indicate the exception (meaning,
there is a separate type to allow you to differentiate the exceptions) as
well as any information the type exposes.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jim Heavey" <Ji*******@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
When an exception is generated is there not suppose to be a property which
contains the error code? I'm not seeing this property. Does it not show
an
error code if the exception comes from a COM component?

Thanks in advance for your assitance.

Jan 25 '06 #2
Yes - but you must have caught a COMException:

try {
// do something
} catch (System.Runtime.InteropServices.COMException ex) { //
COM exception
System.Diagnostics.Debug.WriteLine(ex.GetType().Na me);
System.Diagnostics.Debug.WriteLine(ex.ErrorCode); // HRESULT
throw;
} catch (Exception ex) { // some other exception
System.Diagnostics.Debug.WriteLine(ex.GetType().Na me);
throw;
}

Marc
Jan 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by frazer | last post: by
3 posts views Thread by dhussong | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.