473,406 Members | 2,849 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,406 software developers and data experts.

Exception identifier

Hi all, I have a question about capturing exception details in C#.

Is it possible to query the exception and get an id related to what caused
the exception??

e.g

1 = Tried to enter a row into a database but there was a Primary Key
conflict

Currently I am doing something like this

catch(Exception ex){
if (ex.Message.ToString().ToLower().IndexOf("primary key")>0){

But this looks decidely suspect so if anyone knows of a better way, I am all
ears ;)

Thanks
Mark

Nov 16 '05 #1
3 1872
John wrote:
Hi all, I have a question about capturing exception details in C#.

Is it possible to query the exception and get an id related to what caused
the exception??

e.g

1 = Tried to enter a row into a database but there was a Primary Key
conflict

Currently I am doing something like this

catch(Exception ex){
if (ex.Message.ToString().ToLower().IndexOf("primary key")>0){

But this looks decidely suspect so if anyone knows of a better way, I am all
ears ;)

Thanks
Mark


Define your own exception class derived from Exception like this:
class MyException: Exception
{
int ErrCode;
MyException(int err, string msg): base(msg)
{
ErrCode = err;
}
}

Something like that. And when you catch the exception, you know the err code and explanation.

catch (Exception ex)
{
if (ex is MyException)
if (((MyExcption)ex).ErrCode = 5)
{...do whatever...}
}

Hope it helps
Andrey
Nov 16 '05 #2
Is it possible to query the exception and get an id related to what caused
the exception??


If its a SqlException you're catching, you can get plenty more
information from the Errors property (a collection of SqlError, which
have a numeric Number property).


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3
Thanks, I'll take a look at these ideas
Regards
Mark
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:eY**************@tk2msftngp13.phx.gbl...
Is it possible to query the exception and get an id related to what causedthe exception??


If its a SqlException you're catching, you can get plenty more
information from the Errors property (a collection of SqlError, which
have a numeric Number property).


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #4

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

Similar topics

0
by: Alex M | last post by:
When running an application under weblogic 8.1 in development environment, where all classpaths are set up in the IDE before launching the app server, the application runs fine. When deployed as...
3
by: Don McNamara | last post by:
Hi, I've hit quite a strange problem with XmlSerializer on my W2K3 server. When I serialize/deserialize using an exe on my local computer (XP), everything works fine. When I put the code out on...
0
by: George | last post by:
Hi, The storeprocedure fails with null exception on the DB2 development centre for SELECT querys for all tables that has the number of columns greater than 10. Strange ,but true ..i reinstalled...
6
by: William Stacey | last post by:
Wouldn't it be better if you could throw one of the prebuilt exceptions, such as ApplicationException or ArgumentNullException, and also supply a numeric errorcode with it. Then the users can...
16
by: ChInKPoInt [No MCSD] | last post by:
I am using Visual Studio 2K3 writing a ASP.NET web application. Is there a way to force the C# compiler to catch possible exception? In Java, all exceptions thrown MUST BE caught, otherwise...
1
by: Ramanfromoz | last post by:
Hi, Developing a new we application. Everything okay on my local WIN XP PROFESSIONAL, IIS 5.0 running locally. The website is running smoothly. Now, the same code I am copying over to a...
5
by: aualias | last post by:
I am trying to run the Secure Conversation example in WSE 2.0. On the call to client.IssueSecurityContextTokenAuthenticated(token, issuerToken); I get the following exception: ...
0
by: webmaster | last post by:
Hi all, I'm tearing my hair out with this one. I have successfully implemented by own RadioButtonList in order to provide additional functionality and a DIV rather than TABLE-based layout in...
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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.