.NET Forum / Languages / C# / October 2007
Get Error Number
|
|
Thread rating:  |
RP - 10 Oct 2007 19:04 GMT If an exception occurs, how to get the error number. I mean, is there anything like ex.number?
Jon Skeet [C# MVP] - 10 Oct 2007 19:19 GMT > 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.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Chris Mullins [MVP - C#] - 10 Oct 2007 19:40 GMT >> 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...
-- Chris Mullins
Nicholas Paldino [.NET/C# MVP] - 10 Oct 2007 19:47 GMT 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.
 Signature - Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com
>>> If an exception occurs, how to get the error number. I mean, is there >>> anything like ex.number? [quoted text clipped - 12 lines] > -- > Chris Mullins Peter Bromberg [C# MVP] - 10 Oct 2007 19:55 GMT 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
> 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 [quoted text clipped - 16 lines] > > -- > > Chris Mullins Nicholas Paldino [.NET/C# MVP] - 10 Oct 2007 20:18 GMT 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. =)
 Signature - Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com
> According to Michaelis, "In C# 2.0, all exceptions, whether deriving from > System.Exception or not, will propagate into C# assemblies as derived from [quoted text clipped - 28 lines] >> > -- >> > Chris Mullins Peter Bromberg [C# MVP] - 10 Oct 2007 20:32 GMT 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
> 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 [quoted text clipped - 38 lines] > >> > -- > >> > Chris Mullins RP - 10 Oct 2007 19:56 GMT In VB.NET we have err.Number.
Chris Mullins [MVP - C#] - 10 Oct 2007 20:50 GMT 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
> 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 [quoted text clipped - 17 lines] >> -- >> Chris Mullins John Duval - 10 Oct 2007 19:23 GMT > 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
Ignacio Machin ( .NET/ C# MVP ) - 10 Oct 2007 19:38 GMT Hi,
> 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.
Willy Denoyette [MVP] - 10 Oct 2007 21:46 GMT > 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.
Arne Vajhøj - 12 Oct 2007 01:28 GMT > 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
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|