On Oct 10, 2:04 pm, RP <rpk.gene...@gm ail.comwrote:
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.Er rorCode) 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)?
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.Exceptio n.
>>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
>
>
According to Michaelis, "In C# 2.0, all exceptions, whether deriving from
System.Exceptio n or not, will propagate into C# assemblies as derived from
System.Exceptio n. .... 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 [.NET/C# MVP]" wrote:
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.Exceptio n.
>
>
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard. caspershouse.co m
>
>
"Chris Mullins [MVP - C#]" <cmullins@yahoo .comwrote in message
news:%23KWu2z2C IHA.5976@TK2MSF TNGP02.phx.gbl. ..
RP <rpk.general@gm ail.comwrote:
>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...
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. =)
According to Michaelis, "In C# 2.0, all exceptions, whether deriving from
System.Exceptio n or not, will propagate into C# assemblies as derived from
System.Exceptio n. .... 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 [.NET/C# MVP]" wrote:
>
> 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.Exceptio n.
>>
>>
>--
> - Nicholas Paldino [.NET/C# MVP]
> - mvp@spam.guard. caspershouse.co m
>>
>>
>"Chris Mullins [MVP - C#]" <cmullins@yahoo .comwrote in message
>news:%23KWu2z2 CIHA.5976@TK2MS FTNGP02.phx.gbl ...
"Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
news:MPG.217728 24c9f03b1e51b@m snews.microsoft .com...
>RP <rpk.general@gm ail.comwrote:
>>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
>
>
Yep, you are correct -- in C# 2.0 Chris's example code is not valid - you
would get "type object does not extend System.Exceptio n" in Intellisense, and
a similar compiler error.
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]
- mvp@spam.guard. caspershouse.co m
>
"Peter Bromberg [C# MVP]" <pbromberg@yaho o.yohohhoandabo ttleofrum.comwr ote
in message news:51EC8543-CC2B-4A00-BFE8-01DF6B20428D@mi crosoft.com...
According to Michaelis, "In C# 2.0, all exceptions, whether deriving from
System.Exceptio n or not, will propagate into C# assemblies as derived from
System.Exceptio n. .... 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 [.NET/C# MVP]" wrote:
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.Exceptio n.
>
>
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard. caspershouse.co m
>
>
"Chris Mullins [MVP - C#]" <cmullins@yahoo .comwrote in message
news:%23KWu2z2C IHA.5976@TK2MSF TNGP02.phx.gbl. ..
"Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
news:MPG.217728 24c9f03b1e51b@m snews.microsoft .com...
RP <rpk.general@gm ail.comwrote:
>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...
"RP" <rpk.general@gm ail.comwrote in message
news:1192039451 .246765.219050@ 19g2000hsx.goog legroups.com...
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(Exceptio n);
int v = (int) t.InvokeMember( "HResult",
BindingFlags.De claredOnly |
BindingFlags.No nPublic |
BindingFlags.In stance |
BindingFlags.Ge tProperty, null, ex, null);
Console.WriteLi ne("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("What ever");
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.
Comment