473,320 Members | 1,922 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.

Exception handling

I want to tell the user if, for example, he has chosen a value of the
primary key for a new record already exists. What I do now is do a
try-catch with the sql insert. That prohibits page failure, but I want to
notify the user of the specific result -- other than a generic failure
message. I have searched and searched but I have found no place where there
is a list of the names of all the exceptions (for sql and others). Does
anyone know where it is? It MUST exist somewhere.

Also, is there an automatic way to feed this back to the user other than
having a label that has nothing in it and is populated by the error message?

Shelly
Sep 6 '07 #1
6 2371
"Shelly" <sh************@asap-consult.comwrote in message
news:13*************@corp.supernews.com...
>I want to tell the user if, for example, he has chosen a value of the
primary key for a new record already exists. What I do now is do a
try-catch with the sql insert. That prohibits page failure, but I want to
notify the user of the specific result -- other than a generic failure
message. I have searched and searched but I have found no place where
there is a list of the names of all the exceptions (for sql and others).
Does anyone know where it is? It MUST exist somewhere.
Why should there be a list? What if I add one tomorrow? What if Microsoft
adds one tomorrow?
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

Sep 6 '07 #2
"Shelly" <sh************@asap-consult.comwrote in message
news:13*************@corp.supernews.com...
>I want to tell the user if, for example, he has chosen a value of the
primary key for a new record already exists. What I do now is do a
try-catch with the sql insert. That prohibits page failure, but I want to
notify the user of the specific result -- other than a generic failure
message. I have searched and searched but I have found no place where
there is a list of the names of all the exceptions (for sql and others).
Does anyone know where it is? It MUST exist somewhere.
You're doing this backwards...

One of the first guidelines for exceptions is that you do everything you can
to stop them happening. In this case, you know that there's a strong
likelihood that the way you've written your web app means that it can
produce primary key violations. Therefore, eliminate that possibility and
you can (more or less) forget about it.

SELECT COUNT(*) FROM MyTable WHERE MyPrimaryKey = <value>

If that returns 1 rather than zero, don't even try to insert the record.
Also, is there an automatic way to feed this back to the user other than
having a label that has nothing in it and is populated by the error
message?
pseudo-code follows:

if (CheckIfRecordExists())
{
ClientScript.RegisterStartupScript(GetType(), "recordExists",
"alert('Record already exists!'););
}
else
{
InsertRecord();
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Sep 6 '07 #3

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:O$*************@TK2MSFTNGP02.phx.gbl...
"Shelly" <sh************@asap-consult.comwrote in message
news:13*************@corp.supernews.com...
>>I want to tell the user if, for example, he has chosen a value of the
primary key for a new record already exists. What I do now is do a
try-catch with the sql insert. That prohibits page failure, but I want to
notify the user of the specific result -- other than a generic failure
message. I have searched and searched but I have found no place where
there is a list of the names of all the exceptions (for sql and others).
Does anyone know where it is? It MUST exist somewhere.

You're doing this backwards...

One of the first guidelines for exceptions is that you do everything you
can to stop them happening. In this case, you know that there's a strong
likelihood that the way you've written your web app means that it can
produce primary key violations. Therefore, eliminate that possibility and
you can (more or less) forget about it.

SELECT COUNT(*) FROM MyTable WHERE MyPrimaryKey = <value>

If that returns 1 rather than zero, don't even try to insert the record.
Of course that is the way to do it for this example. However, I only used
that as an example and there are other exceptions that should be trappable
(and not just in SQL). There should be a list somewhere.

In answer to the other responder (John Saunders), it is quite true that the
list is expandable. However, if YOU expand the list, then YOU know how it
was expanded. What I am taling about is the base list. Eons ago when I was
programming for VMS, we could add our own errors to a base list -- but there
WAS a base list published.
>
>Also, is there an automatic way to feed this back to the user other than
having a label that has nothing in it and is populated by the error
message?

pseudo-code follows:

if (CheckIfRecordExists())
{
ClientScript.RegisterStartupScript(GetType(), "recordExists",
"alert('Record already exists!'););
}
else
{
InsertRecord();
}
Thanks.

Shelly
Sep 6 '07 #4

I would read this article on DotNet exception handling.

http://blogs.msdn.com/kcwalina/archi...16/396787.aspx
It's a little trickier to handle Sql Server errors, but "exceptions should
be exceptional", and if you expect this behavior from time to time, then you
should handle it gracefully, rather than catching certain number exceptions.


"Shelly" <sh************@asap-consult.comwrote in message
news:13*************@corp.supernews.com...
>I want to tell the user if, for example, he has chosen a value of the
primary key for a new record already exists. What I do now is do a
try-catch with the sql insert. That prohibits page failure, but I want to
notify the user of the specific result -- other than a generic failure
message. I have searched and searched but I have found no place where
there is a list of the names of all the exceptions (for sql and others).
Does anyone know where it is? It MUST exist somewhere.

Also, is there an automatic way to feed this back to the user other than
having a label that has nothing in it and is populated by the error
message?

Shelly

Sep 6 '07 #5
"Shelly" <sh************@asap-consult.comwrote in message
news:13*************@corp.supernews.com...
Of course that is the way to do it for this example.
The same guideline applies to any other example - if an exception is likely
to happen, then it's not an exception...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Sep 6 '07 #6
"Shelly" <sh************@asap-consult.comwrote in message
news:13*************@corp.supernews.com...
>
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:O$*************@TK2MSFTNGP02.phx.gbl...
>"Shelly" <sh************@asap-consult.comwrote in message
news:13*************@corp.supernews.com...
In answer to the other responder (John Saunders), it is quite true that
the list is expandable. However, if YOU expand the list, then YOU know
how it was expanded. What I am taling about is the base list. Eons ago
when I was programming for VMS, we could add our own errors to a base
list -- but there WAS a base list published.
Wrong. This was a mistake in VMS, for the same reason. There was a list
published, but that list was subject to be changed at any time. In any
release at all, even in a patch release, new error codes could be added. At
any time at all, the text of the associated error message could be changed.
I used to joke that we should change the error text every release, just to
catch customers who were depending on the text of the error messages.

So, no, this is not the way to do it. If you'll give us a better idea of
what you're trying to accomplish, we may be able to help you. Especially
those of us whose experience spans KA10 to Pentium-n.
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

Sep 6 '07 #7

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

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
2
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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.