473,320 Members | 1,965 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,320 software developers and data experts.

How to get the Exception number

RSB
Hi Every one,
i am using the try Catch block..
and the Exception object has a Message Property but i want to Catch the
Error Number so that based on the Error number i can display Different error
message....

try{

}

Catch (Exception ex)
{
errNum = ex.?????
if (errNum == x) {
do x
}
else if ( errNum == y) {
do y
}
}
so how do i find the Error Number here..

Thanks
RSB
Nov 21 '05 #1
7 23411
RSB <rs*****@hotmail.com> wrote:
i am using the try Catch block..
and the Exception object has a Message Property but i want to Catch the
Error Number so that based on the Error number i can display Different error
message....

try{

}

Catch (Exception ex)
{
errNum = ex.?????
if (errNum == x) {
do x
}
else if ( errNum == y) {
do y
}
}
so how do i find the Error Number here..


Exception.HResult may be what you're after. Can you definitely not
achieve this by catching different types of exception though? That's
the usual practice.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 21 '05 #2
Hi,

There is no such beast (take a look at the Exception base class, at its
property and method... if it is not there, ... it is not there).

You should use the Exception derived class, from the particular to the
most general:

catch(ArgumentOutOfRangeException ex)
{
...
}
catch( ArgumentException ex)
{
...
}
catch (SystemException ex)
{
...
}

and so on.

You can "parse" the Exception.Message, or read the
Exception.InnerException, or StackTrace, etc. to get more info about the
nature of the error, but a try-catch is, by nature, a termination model, not
a resumption model of error handling... you loose the context, the
environment of the error, since the try scope is done, finish, out, dead.

You can throw a new exception, with a new message, of with a message
based on the existing message.

Hoping it may help,
Vanderghast, Access MVP
"RSB" <rs*****@hotmail.com> wrote in message
news:U2*****************@news.cpqcorp.net...
Hi Every one,
i am using the try Catch block..
and the Exception object has a Message Property but i want to Catch the
Error Number so that based on the Error number i can display Different error message....

try{

}

Catch (Exception ex)
{
errNum = ex.?????
if (errNum == x) {
do x
}
else if ( errNum == y) {
do y
}
}
so how do i find the Error Number here..

Thanks
RSB

Nov 21 '05 #3
RSB
Thanks Jon and Michel
that helps me..

"Michel Walsh" <vanderghast@VirusAreFunnierThanSpam> wrote in message
news:ek**************@TK2MSFTNGP11.phx.gbl...
Hi,

There is no such beast (take a look at the Exception base class, at its property and method... if it is not there, ... it is not there).

You should use the Exception derived class, from the particular to the
most general:

catch(ArgumentOutOfRangeException ex)
{
...
}
catch( ArgumentException ex)
{
...
}
catch (SystemException ex)
{
...
}

and so on.

You can "parse" the Exception.Message, or read the
Exception.InnerException, or StackTrace, etc. to get more info about the
nature of the error, but a try-catch is, by nature, a termination model, not a resumption model of error handling... you loose the context, the
environment of the error, since the try scope is done, finish, out, dead.

You can throw a new exception, with a new message, of with a message
based on the existing message.

Hoping it may help,
Vanderghast, Access MVP
"RSB" <rs*****@hotmail.com> wrote in message
news:U2*****************@news.cpqcorp.net...
Hi Every one,
i am using the try Catch block..
and the Exception object has a Message Property but i want to Catch the
Error Number so that based on the Error number i can display Different

error
message....

try{

}

Catch (Exception ex)
{
errNum = ex.?????
if (errNum == x) {
do x
}
else if ( errNum == y) {
do y
}
}
so how do i find the Error Number here..

Thanks
RSB


Nov 21 '05 #4
RSB
So how do i cappture..
Object reference not set to an instance of an object.
i mean what type of Exception i catch there.

thanks

"Michel Walsh" <vanderghast@VirusAreFunnierThanSpam> wrote in message
news:ek**************@TK2MSFTNGP11.phx.gbl...
Hi,

There is no such beast (take a look at the Exception base class, at its property and method... if it is not there, ... it is not there).

You should use the Exception derived class, from the particular to the
most general:

catch(ArgumentOutOfRangeException ex)
{
...
}
catch( ArgumentException ex)
{
...
}
catch (SystemException ex)
{
...
}

and so on.

You can "parse" the Exception.Message, or read the
Exception.InnerException, or StackTrace, etc. to get more info about the
nature of the error, but a try-catch is, by nature, a termination model, not a resumption model of error handling... you loose the context, the
environment of the error, since the try scope is done, finish, out, dead.

You can throw a new exception, with a new message, of with a message
based on the existing message.

Hoping it may help,
Vanderghast, Access MVP
"RSB" <rs*****@hotmail.com> wrote in message
news:U2*****************@news.cpqcorp.net...
Hi Every one,
i am using the try Catch block..
and the Exception object has a Message Property but i want to Catch the
Error Number so that based on the Error number i can display Different

error
message....

try{

}

Catch (Exception ex)
{
errNum = ex.?????
if (errNum == x) {
do x
}
else if ( errNum == y) {
do y
}
}
so how do i find the Error Number here..

Thanks
RSB


Nov 21 '05 #5
RSB,

If you are trying to detect when a variable is set to null, and a
property/method/field is trying to be accessed, then look for the
NullReferenceException.

Hope this helps.

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

"RSB" <rs*****@hotmail.com> wrote in message
news:JO*****************@news.cpqcorp.net...
So how do i cappture..
Object reference not set to an instance of an object.
i mean what type of Exception i catch there.

thanks

"Michel Walsh" <vanderghast@VirusAreFunnierThanSpam> wrote in message
news:ek**************@TK2MSFTNGP11.phx.gbl...
Hi,

There is no such beast (take a look at the Exception base class, at

its
property and method... if it is not there, ... it is not there).

You should use the Exception derived class, from the particular to
the
most general:

catch(ArgumentOutOfRangeException ex)
{
...
}
catch( ArgumentException ex)
{
...
}
catch (SystemException ex)
{
...
}

and so on.

You can "parse" the Exception.Message, or read the
Exception.InnerException, or StackTrace, etc. to get more info about the
nature of the error, but a try-catch is, by nature, a termination model,

not
a resumption model of error handling... you loose the context, the
environment of the error, since the try scope is done, finish, out, dead.

You can throw a new exception, with a new message, of with a message
based on the existing message.

Hoping it may help,
Vanderghast, Access MVP
"RSB" <rs*****@hotmail.com> wrote in message
news:U2*****************@news.cpqcorp.net...
> Hi Every one,
> i am using the try Catch block..
> and the Exception object has a Message Property but i want to Catch the
> Error Number so that based on the Error number i can display Different

error
> message....
>
> try{
>
> }
>
> Catch (Exception ex)
> {
> errNum = ex.?????
> if (errNum == x) {
> do x
> }
> else if ( errNum == y) {
> do y
> }
> }
>
>
> so how do i find the Error Number here..
>
> Thanks
> RSB
>
>



Nov 21 '05 #6
Hi,
Sounds to me to be a System.NullRefereneException. At least, the
following code does:

try
{
string a = null;
int i = a.Length;
}
catch(NullReferenceException ex)
{
return;
}
catch(Exception ex)
{
return;
}
Place a stop point at each return, start a debug session. If you got
stop at the right return, no problem, you have the intended exception, else,
the catch(Exception ex) should display, in the Locals view, under variable
ex, the exception class you are looking for. Not very high tech, I admit my
culpability...
Hoping it may help
Vanderghast, Access MVP
"RSB" <rs*****@hotmail.com> wrote in message
news:JO*****************@news.cpqcorp.net...
So how do i cappture..
Object reference not set to an instance of an object.
i mean what type of Exception i catch there.

thanks

"Michel Walsh" <vanderghast@VirusAreFunnierThanSpam> wrote in message
news:ek**************@TK2MSFTNGP11.phx.gbl...
Hi,

There is no such beast (take a look at the Exception base class, at

its
property and method... if it is not there, ... it is not there).

You should use the Exception derived class, from the particular to the most general:

catch(ArgumentOutOfRangeException ex)
{
...
}
catch( ArgumentException ex)
{
...
}
catch (SystemException ex)
{
...
}

and so on.

You can "parse" the Exception.Message, or read the
Exception.InnerException, or StackTrace, etc. to get more info about the nature of the error, but a try-catch is, by nature, a termination model,

not
a resumption model of error handling... you loose the context, the
environment of the error, since the try scope is done, finish, out, dead.
You can throw a new exception, with a new message, of with a message
based on the existing message.

Hoping it may help,
Vanderghast, Access MVP
"RSB" <rs*****@hotmail.com> wrote in message
news:U2*****************@news.cpqcorp.net...
Hi Every one,
i am using the try Catch block..
and the Exception object has a Message Property but i want to Catch the Error Number so that based on the Error number i can display Different

error
message....

try{

}

Catch (Exception ex)
{
errNum = ex.?????
if (errNum == x) {
do x
}
else if ( errNum == y) {
do y
}
}
so how do i find the Error Number here..

Thanks
RSB



Nov 21 '05 #7
RSB <rs*****@hotmail.com> wrote:
So how do i cappture..
Object reference not set to an instance of an object.
i mean what type of Exception i catch there.


NullReferenceException.

(If you can get to the message, surely you can also see what type of
exception you've got, can't you?)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 21 '05 #8

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

Similar topics

7
by: RSB | last post by:
Hi Every one, i am using the try Catch block.. and the Exception object has a Message Property but i want to Catch the Error Number so that based on the Error number i can display Different error...
3
by: shajeel | last post by:
i got this exception while calling Timer.Start() Number must be either non-negative or -1. Parameter name: dueTime can any one tell me the reason. timer interval is set to 180000. and after...
2
by: soniyavk | last post by:
how to get exception number in c#(not line number)
2
by: =?Utf-8?B?Umljaw==?= | last post by:
How to I get the Exception Number (code) for a database related error? I get the following error and I need to catch it to display a custom message. System.InvalidCastException: Unable to cast...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.