By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,990 Members | 2,138 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,990 IT Pros & Developers. It's quick & easy.

How to get the Exception number

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.