Get Error Number

  • Thread starter Thread starter RP
  • Start date Start date
R

RP

If an exception occurs, how to get the error number. I mean, is there
anything like ex.number?
 
RP said:
If an exception occurs, how to get the error number. I mean, is there
anything like ex.number?

For many exceptions there *isn't* an error number - after all, you can
create whatever exception you want and throw it.

You can use Exception.HResult in some cases, but don't rely on it
always being present.
 
If an exception occurs, how to get the error number. I mean, is there
anything like ex.number?

Hi RP,
There really isn't anything equivalent to an error number in .NET --
typically the type of the exception object thrown is what is used to
do runtime error handling. And the message in the exception is
typically what is shown to the user, not a number. There are some
types of exceptions that have error numbers (see
COMException.ErrorCode) but I think in the new .NET world, the error
number has gone by the wayside.

Is there a particular case where you need to get at the error number
(a la GetLastError from the Win32 days)?

John
 
Hi,

RP said:
If an exception occurs, how to get the error number. I mean, is there
anything like ex.number?

Not really, you can get the message of the exception and even the stacktrace
that indicate in which method the exception was thrown.
 
Jon Skeet said:
For many exceptions there *isn't* an error number - after all, you can
create whatever exception you want and throw it.

heh. Perfectly valid, if not quite correct, code...

Object errorNumber = (object) 101;
throw errorNumber;

One day I'm going to figure out why any arbitrary thing can be thrown,
rather than constraining it to only Exceptions...
 
Actually, it's not valid C# code. While the CLR does allow for objects
to be thrown, in C#, the compiler requires that the target of the throw
statement derive from System.Exception.
 
According to Michaelis, "In C# 2.0, all exceptions, whether deriving from
System.Exception or not, will propagate into C# assemblies as derived from
System.Exception. .... the CIL code corresponding to an empty catch block is,
in fact, a catch(object) block..."
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com



Nicholas Paldino said:
Actually, it's not valid C# code. While the CLR does allow for objects
to be thrown, in C#, the compiler requires that the target of the throw
statement derive from System.Exception.
 
Which is correct, but it doesn't validate Chris's post, which indicates
that you can actually throw something in C# which does not derive from
Exception.

You could do this in C++/CLI or in IL, but not in C#. And yes, you can
catch it with the catch block which doesn't have an exception argument.

I actually did some of the review work on that book, and know Mark, so I
know exactly which section you are referring to. =)

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Bromberg said:
According to Michaelis, "In C# 2.0, all exceptions, whether deriving from
System.Exception or not, will propagate into C# assemblies as derived from
System.Exception. .... the CIL code corresponding to an empty catch block
is,
in fact, a catch(object) block..."
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com



Nicholas Paldino said:
Actually, it's not valid C# code. While the CLR does allow for
objects
to be thrown, in C#, the compiler requires that the target of the throw
statement derive from System.Exception.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Chris Mullins said:
If an exception occurs, how to get the error number. I mean, is there
anything like ex.number?

For many exceptions there *isn't* an error number - after all, you can
create whatever exception you want and throw it.

heh. Perfectly valid, if not quite correct, code...

Object errorNumber = (object) 101;
throw errorNumber;

One day I'm going to figure out why any arbitrary thing can be thrown,
rather than constraining it to only Exceptions...
 
Yep, you are correct -- in C# 2.0 Chris's example code is not valid - you
would get "type object does not extend System.Exception" in Intellisense, and
a similar compiler error.

-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com



Nicholas Paldino said:
Which is correct, but it doesn't validate Chris's post, which indicates
that you can actually throw something in C# which does not derive from
Exception.

You could do this in C++/CLI or in IL, but not in C#. And yes, you can
catch it with the catch block which doesn't have an exception argument.

I actually did some of the review work on that book, and know Mark, so I
know exactly which section you are referring to. =)

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Bromberg said:
According to Michaelis, "In C# 2.0, all exceptions, whether deriving from
System.Exception or not, will propagate into C# assemblies as derived from
System.Exception. .... the CIL code corresponding to an empty catch block
is,
in fact, a catch(object) block..."
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com



Nicholas Paldino said:
Actually, it's not valid C# code. While the CLR does allow for
objects
to be thrown, in C#, the compiler requires that the target of the throw
statement derive from System.Exception.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


If an exception occurs, how to get the error number. I mean, is there
anything like ex.number?

For many exceptions there *isn't* an error number - after all, you can
create whatever exception you want and throw it.

heh. Perfectly valid, if not quite correct, code...

Object errorNumber = (object) 101;
throw errorNumber;

One day I'm going to figure out why any arbitrary thing can be thrown,
rather than constraining it to only Exceptions...
 
How very strange.

I have very clear memories of reading that somewhere, thinking, "No Way",
trying it, and having it work.

But I just tried it in VB & C#, in VS 2003/2005/2008, and it falied in all 6
cases. Ah well, back to the institution for me...

--
Chris Mullins

Nicholas Paldino said:
Actually, it's not valid C# code. While the CLR does allow for objects
to be thrown, in C#, the compiler requires that the target of the throw
statement derive from System.Exception.
 
RP said:
If an exception occurs, how to get the error number. I mean, is there
anything like ex.number?


You can always retrieve the HRsesult property, but this requires to reflect
on a protected property [1], so I would advise against it in production
code.

[1]
catch(Exception ex)
{
Type t = typeof(Exception);
int v = (int) t.InvokeMember("HResult",
BindingFlags.DeclaredOnly |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.GetProperty, null, ex, null);

Console.WriteLine("HResult: {0:x}", v);
}

Following will output HResult: 80131508

try {
int[] ia = new int[2];
ia[2] = 1;
}
include [1] here.

while throw new Exception("Whatever");

will output HResult: 80131500, which is the HResult code for CLR Exception.
Note that you can define your own HResult codes (respecting the Win32/COM
rules applicable to HRESULT codes) to be used in your Exception derived
classes.
 
RP said:
If an exception occurs, how to get the error number. I mean, is there
anything like ex.number?

The information about the type of exception is carried
in the type of the exception.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top