473,320 Members | 1,952 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 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 5428
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**********@hotmail.com> wrote in message
news:Ol**************@TK2MSFTNGP10.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.SqlClient.SqlException 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 WriteErrorMessage ("Fail to do this op, SQL error desription" +
exp.Message);
}
catch (Exception exp)
{
WriteErrorMessage ("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**********@hotmail.com> wrote in message
news:Ol**************@TK2MSFTNGP10.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.com> wrote in message
news:ed**************@tk2msftngp13.phx.gbl...
Hi,
If it is SQL error, you may catch System.Data.SqlClient.SqlException 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 WriteErrorMessage ("Fail to do this op, SQL error desription" +
exp.Message);
}
catch (Exception exp)
{
WriteErrorMessage ("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**********@hotmail.com> wrote in message
news:Ol**************@TK2MSFTNGP10.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.Number -- 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**********@hotmail.com> wrote in message
news:Ol**************@TK2MSFTNGP10.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*****@nospam-digita.com> wrote in message
news:eo**************@TK2MSFTNGP12.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.Number -- 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**********@hotmail.com> wrote in message
news:Ol**************@TK2MSFTNGP10.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
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...
7
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: -------------...
3
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...
6
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
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
0
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.