473,513 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2392
"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
2837
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
2322
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
5969
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2736
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
2582
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
2523
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
4162
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
5110
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
3022
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
3089
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
7260
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
7160
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
7537
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
7525
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...
1
5086
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...
0
4746
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...
0
1594
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 ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
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...

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.