473,785 Members | 3,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to identify an error?

Hi,

I want my application do different actions depending on the exception it
gets.
For exemple: I have an SQL-table with a unique index. In case I try to
Insert a record that's alreaddy in it I get this exception: "Cannot insert
duplicate key row in object 'tblTelephones' with unique index
'UniqueValues'. "

What I'm looking for is a way to identify the exception: in case I get this
exception I want to do this, in case of another I want to do that.

The most simple solution to me seems this:
Catch ex As Exception
If Left(ex.Message , 41) <> "Cannot insert duplicate key row in
object" Then
'do this action
End If

QAlthough, I'm not convinced this is the best way. Is there a way to
identify the exception with a unique number? Or I've seen once something
like a name for an error. Can anybody help me with this?

Thanks a lot in advance

Pieter

Nov 22 '05 #1
5 5463
One way around this is to set the column seed to -1 and count down. This way
when the new records are inserted you will not get duplicates assuming your
records are all positive.

However, I would recommend that if possible you dump this idea and use
GUID's which are extremely reliable as far a uniqueness is concerned ( not
totally though ).

Regards - OHM

"DraguVaso" <pi**********@h otmail.com> wrote in message
news:Ol******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

I want my application do different actions depending on the exception it
gets.
For exemple: I have an SQL-table with a unique index. In case I try to
Insert a record that's alreaddy in it I get this exception: "Cannot insert
duplicate key row in object 'tblTelephones' with unique index
'UniqueValues'. "

What I'm looking for is a way to identify the exception: in case I get this exception I want to do this, in case of another I want to do that.

The most simple solution to me seems this:
Catch ex As Exception
If Left(ex.Message , 41) <> "Cannot insert duplicate key row in
object" Then
'do this action
End If

QAlthough, I'm not convinced this is the best way. Is there a way to
identify the exception with a unique number? Or I've seen once something
like a name for an error. Can anybody help me with this?

Thanks a lot in advance

Pieter

Nov 22 '05 #2
Tal
Hi,
If it is SQL error, you may catch System.Data.Sql Client.SqlExcep tion instead
of Exception.
In SqlException, you are having several properties such as Number that give
you the specific error.

So in the client side you may want to do something like that:
catch (SqlException exp)
{
if (exp.Number == 1801)
do action;
else if (exp.Number == 1802)
do action2;
else WriteErrorMessa ge ("Fail to do this op, SQL error desription" +
exp.Message);
}
catch (Exception exp)
{
WriteErrorMessa ge ("Fail to do this op, error desription" +
exp.Message);
}

You may also define your excpetions and do the following in the server side:
catch (SqlException exp)
{
if (exp.Number == 1801)
throw new MyException1();
else if (exp.Number == 1802)
throw new MyException2();
else throw;
}

And in the client side catch those exceptions and do the needed actions.

It is not a good idea to have if statement on the message string.

"DraguVaso" <pi**********@h otmail.com> wrote in message
news:Ol******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

I want my application do different actions depending on the exception it
gets.
For exemple: I have an SQL-table with a unique index. In case I try to
Insert a record that's alreaddy in it I get this exception: "Cannot insert
duplicate key row in object 'tblTelephones' with unique index
'UniqueValues'. "

What I'm looking for is a way to identify the exception: in case I get this exception I want to do this, in case of another I want to do that.

The most simple solution to me seems this:
Catch ex As Exception
If Left(ex.Message , 41) <> "Cannot insert duplicate key row in
object" Then
'do this action
End If

QAlthough, I'm not convinced this is the best way. Is there a way to
identify the exception with a unique number? Or I've seen once something
like a name for an error. Can anybody help me with this?

Thanks a lot in advance

Pieter

Nov 22 '05 #3
Thanks Tal! That was exactly what I needed!

"Tal" <ta**@nice.co m> wrote in message
news:ed******** ******@tk2msftn gp13.phx.gbl...
Hi,
If it is SQL error, you may catch System.Data.Sql Client.SqlExcep tion instead of Exception.
In SqlException, you are having several properties such as Number that give you the specific error.

So in the client side you may want to do something like that:
catch (SqlException exp)
{
if (exp.Number == 1801)
do action;
else if (exp.Number == 1802)
do action2;
else WriteErrorMessa ge ("Fail to do this op, SQL error desription" +
exp.Message);
}
catch (Exception exp)
{
WriteErrorMessa ge ("Fail to do this op, error desription" +
exp.Message);
}

You may also define your excpetions and do the following in the server side: catch (SqlException exp)
{
if (exp.Number == 1801)
throw new MyException1();
else if (exp.Number == 1802)
throw new MyException2();
else throw;
}

And in the client side catch those exceptions and do the needed actions.

It is not a good idea to have if statement on the message string.

"DraguVaso" <pi**********@h otmail.com> wrote in message
news:Ol******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

I want my application do different actions depending on the exception it
gets.
For exemple: I have an SQL-table with a unique index. In case I try to
Insert a record that's alreaddy in it I get this exception: "Cannot insert duplicate key row in object 'tblTelephones' with unique index
'UniqueValues'. "

What I'm looking for is a way to identify the exception: in case I get

this
exception I want to do this, in case of another I want to do that.

The most simple solution to me seems this:
Catch ex As Exception
If Left(ex.Message , 41) <> "Cannot insert duplicate key row in object" Then
'do this action
End If

QAlthough, I'm not convinced this is the best way. Is there a way to
identify the exception with a unique number? Or I've seen once something
like a name for an error. Can anybody help me with this?

Thanks a lot in advance

Pieter


Nov 22 '05 #4
For reference, here is how I solved this problem. We had a requirement in a
multi-user system to identify the field that failed (since we can have more
than one unique index per table and it's nice to take the user to the
offending input box). We can't avoid the problem using GUIDs because the
value is human-enetered and if there's more than one user you can get
overlapping edits.

First of all it's best to catch the smallest set of exceptions -- so in this
case we want to catch SqlExceptions and take action on that.

Second, identify the type of error based on the SqlException.Nu mber -- for
example, unique key violation is 2627, and foreign key violation is 547. You
can get these from SQL BOL.

Next is the kluge -- I want to know the table and field so that I can use it
further up the stack if I recognise it. I wrote a dispatcher class which
matches on a regex (eg "Violation of UNIQUE KEY constraint
'(?<CONSTRAINT> .*?)'"). I have one instance per type of violation (unique,
foreign, etc).

So basically, if I have a SqlException, I switch on number. Depending on the
number, I ask the appropriate dispatcher to handle the error message. The
dispatcher allows registration based on the name of the key and a function
to call (which normally throws a typed exception filled out with appropriate
parameters). Since our database generation scripts (and DA layer) are
generated from a single XML schema definition I know what the names of the
keys will be. The dispatcher uses the regex and compares against the
registered keys, and calls the given callback. If there's no match then the
exception is re-thrown and hopefully some generic exception handler gets it
(ie if it works nicely the user is taken to the field; if not then they get
some generic error message).

Pros:
+ I can add translations to specific exceptions only where I need
them -- everything else just bubbles up as usual.
+ Registration is the responsibility of individual business object
classes, not some central point.

Cons:
- Matches are based on human-readable strings, so different languages
and/or versions of the server could break it.

Hope that helps,
Stu
"DraguVaso" <pi**********@h otmail.com> wrote in message
news:Ol******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

I want my application do different actions depending on the exception it
gets.
For exemple: I have an SQL-table with a unique index. In case I try to
Insert a record that's alreaddy in it I get this exception: "Cannot insert
duplicate key row in object 'tblTelephones' with unique index
'UniqueValues'. "

What I'm looking for is a way to identify the exception: in case I get this exception I want to do this, in case of another I want to do that.

The most simple solution to me seems this:
Catch ex As Exception
If Left(ex.Message , 41) <> "Cannot insert duplicate key row in
object" Then
'do this action
End If

QAlthough, I'm not convinced this is the best way. Is there a way to
identify the exception with a unique number? Or I've seen once something
like a name for an error. Can anybody help me with this?

Thanks a lot in advance

Pieter

Nov 22 '05 #5
Stu,
Next is the kluge -- I want to know the table and field so that I can use it further up the stack if I recognise it. I wrote a dispatcher class which
matches on a regex (eg "Violation of UNIQUE KEY constraint
'(?<CONSTRAINT> .*?)'"). I have one instance per type of violation (unique,
foreign, etc).
Have you considered a stored procedure that intelligently checked the
columns and returned (via output parameters) the columns with the
duplicates?
parameters). Since our database generation scripts (and DA layer) are
generated from a single XML schema definition I know what the names of the Which may not be possible...

Hope this helps
Jay
"Stu Smith" <st*****@nosp am-digita.com> wrote in message
news:eo******** ******@TK2MSFTN GP12.phx.gbl... For reference, here is how I solved this problem. We had a requirement in a multi-user system to identify the field that failed (since we can have more than one unique index per table and it's nice to take the user to the
offending input box). We can't avoid the problem using GUIDs because the
value is human-enetered and if there's more than one user you can get
overlapping edits.

First of all it's best to catch the smallest set of exceptions -- so in this case we want to catch SqlExceptions and take action on that.

Second, identify the type of error based on the SqlException.Nu mber -- for
example, unique key violation is 2627, and foreign key violation is 547. You can get these from SQL BOL.

Next is the kluge -- I want to know the table and field so that I can use it further up the stack if I recognise it. I wrote a dispatcher class which
matches on a regex (eg "Violation of UNIQUE KEY constraint
'(?<CONSTRAINT> .*?)'"). I have one instance per type of violation (unique,
foreign, etc).

So basically, if I have a SqlException, I switch on number. Depending on the number, I ask the appropriate dispatcher to handle the error message. The
dispatcher allows registration based on the name of the key and a function
to call (which normally throws a typed exception filled out with appropriate parameters). Since our database generation scripts (and DA layer) are
generated from a single XML schema definition I know what the names of the
keys will be. The dispatcher uses the regex and compares against the
registered keys, and calls the given callback. If there's no match then the exception is re-thrown and hopefully some generic exception handler gets it (ie if it works nicely the user is taken to the field; if not then they get some generic error message).

Pros:
+ I can add translations to specific exceptions only where I need
them -- everything else just bubbles up as usual.
+ Registration is the responsibility of individual business object
classes, not some central point.

Cons:
- Matches are based on human-readable strings, so different languages
and/or versions of the server could break it.

Hope that helps,
Stu
"DraguVaso" <pi**********@h otmail.com> wrote in message
news:Ol******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

I want my application do different actions depending on the exception it
gets.
For exemple: I have an SQL-table with a unique index. In case I try to
Insert a record that's alreaddy in it I get this exception: "Cannot insert duplicate key row in object 'tblTelephones' with unique index
'UniqueValues'. "

What I'm looking for is a way to identify the exception: in case I get

this
exception I want to do this, in case of another I want to do that.

The most simple solution to me seems this:
Catch ex As Exception
If Left(ex.Message , 41) <> "Cannot insert duplicate key row in object" Then
'do this action
End If

QAlthough, I'm not convinced this is the best way. Is there a way to
identify the exception with a unique number? Or I've seen once something
like a name for an error. Can anybody help me with this?

Thanks a lot in advance

Pieter


Nov 22 '05 #6

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

Similar topics

8
487
by: DraguVaso | last post by:
Hi, I want my application do different actions depending on the exception it gets. For exemple: I have an SQL-table with a unique index. In case I try to Insert a record that's alreaddy in it I get this exception: "Cannot insert duplicate key row in object 'tblTelephones' with unique index 'UniqueValues'." What I'm looking for is a way to identify the exception: in case I get this
7
2152
by: Grahmmer | last post by:
I have a few timers that are added to a form at runtime. I can handle the event fine, but I cannot identify which timer fired. Is there a way to do this? Timer Creation: ------------- ....some code... Dim usersTimers(4) As System.Windows.Forms.Timer For i = 0 To 4
3
2603
by: Michel | last post by:
Hi, I wrote an app in .Net and I whant only 1 instance of this app open for the user; the user open my app, do some works and try to open another instance of my app, I whant to show a message to user to inform him that only one instance is permit and then close the second instance after that. I am able to do this when the user run the application on his PC whit this : Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName) ...
6
2496
by: Pieter | last post by:
Hi, For some procedures that throws exceptions, I would like to show different messages to the user depending on what type of exception he's getting. For instance this one: when the file is locked, I want a messagebox to tell that the user has to close the file first. Is there a way to identify an exception by some kind of unique number or something like this? I don't want to identify it on the Exception Message because some users are...
0
10319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.