473,396 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Get Error Number

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

Oct 10 '07 #1
11 4752
On Oct 10, 2:04 pm, RP <rpk.gene...@gmail.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.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

Oct 10 '07 #2
RP <rp*********@gmail.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.

You can use Exception.HResult in some cases, but don't rely on it
always being present.

--
Jon Skeet - <sk***@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
Oct 10 '07 #3
Hi,

"RP" <rp*********@gmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
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.

Oct 10 '07 #4
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
RP <rp*********@gmail.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
Oct 10 '07 #5
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]
- mv*@spam.guard.caspershouse.com
"Chris Mullins [MVP - C#]" <cm******@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
>RP <rp*********@gmail.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


Oct 10 '07 #6
RP
In VB.NET we have err.Number.

Oct 10 '07 #7
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 [.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.Exception.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Chris Mullins [MVP - C#]" <cm******@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
RP <rp*********@gmail.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


Oct 10 '07 #8
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]
- mv*@spam.guard.caspershouse.com

"Peter Bromberg [C# MVP]" <pb*******@yahoo.yohohhoandabottleofrum.comwrote
in message news:51**********************************@microsof t.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
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 [.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.Exception.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Chris Mullins [MVP - C#]" <cm******@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
RP <rp*********@gmail.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




Oct 10 '07 #9
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Peter Bromberg [C# MVP]" <pb*******@yahoo.yohohhoandabottleofrum.comwrote
in message news:51**********************************@microsof t.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
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 [.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.Exception.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Chris Mullins [MVP - C#]" <cm******@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
RP <rp*********@gmail.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




Oct 10 '07 #10
"RP" <rp*********@gmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.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(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.
Oct 10 '07 #11
RP wrote:
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
Oct 12 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mark Depenbrock | last post by:
--Apple-Mail-2--68472726 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed Can not make mysql connection - error log: 030708 08:53:48 mysqld started
9
by: Mark Twombley | last post by:
Hi, I'm just getting back into C++ and had a question about the best practice for assigning error numbers. I have been working in VB for sometime now and there you would start assigning error...
1
by: dawatson833 | last post by:
I want to set an alert for a specific table whenever an event has caused a deadlock to occur on the table. I understand how to set up an alert. But I don't know which error number to use for...
1
by: sunil | last post by:
hello folks, Is there a way to get the error number in VB.net Exception class as it was there in VB6 like 'vb code if err.Number=1234 then 'some code here end if
4
by: amywolfie | last post by:
Hi All: In FileMaker Pro, the error number for "no records found" is 401. What is the equivalent error number in Access (2003)? I want to capture for the "On Open" Event of a report. If...
2
by: dixie | last post by:
Can someone please give me the error number for this error? dixie
2
by: Dixie | last post by:
I have an Access Error "Permission Denied" which is generated when I try to delete a folder with a locked file inside. Can anyone tell me the error number for this error? dixie
0
by: alex21 | last post by:
I'm trying to detect the http status number such as (401 Unauthorized) from a 'WebException' when a WebClient in my code fails. Public Function DataSources_ValidURL() As Boolean Dim...
6
doma23
by: doma23 | last post by:
Error number: -214217887 Description: Das Feld ist zu klein fur die Datenmenge, die Sie hinzufungen wollten. Versuchen Sie, weniger Daten einzufugen. Hi, I've made a particular tool by using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.