473,396 Members | 2,033 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.

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 18 '05 #1
7 1062
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 18 '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 18 '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 18 '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 18 '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 18 '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 18 '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 18 '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...
4
by: maricel | last post by:
I have the following base table structure - DDL: CREATE TABLE "ADMINISTRATOR"."T1" ( "C1" INTEGER NOT NULL ) IN "TEST_TS" ; ALTER TABLE "ADMINISTRATOR"."T1" ADD PRIMARY KEY
5
by: juergen perlinger | last post by:
Hello out there. sometimes I need to have proper control of the floating point arithmetic of the C(and C++) runtime system, and using the f.p. exception handling of the C99 standard is quite...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
2
by: £ukasz | last post by:
Hi, I'm writing application which is drawing an icon. Then it's displayed in tray by NotifyIcon. And there is a problem, because sometimes an exception is thrown: Type:...
2
by: André Nogueira | last post by:
Hi there! I am developing an Windows Explorer-like application that will tell you the size of any folder. But so far I have come across some problems. For instance, you may not have access to all...
2
by: Torben Laursen | last post by:
I handle errors in my code by using my own exception class that takes a number as argument to the constructor, and that number puts a string into the exception class. The user can then catch the...
2
by: Richard Collette | last post by:
Hi, I have a service, that runs perfectly when executed outside of the web service environment. When called as a web service I get the exception listed below sporadically. A call to the web...
1
by: Jarsinio | last post by:
I have some doubts about how managed exception in a VB.net aplication. In my aplicattion I have 3 clases: One clase is the UI ( Windows Forms). This class call a second clase who perform an...
4
by: Ed Dana | last post by:
I am attempting to use exception handling in the console to trap an invalid character into a numeric field. Unfortunately, using this example:...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.