473,587 Members | 2,579 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 2416
"Shelly" <sh************ @asap-consult.comwrot e in message
news:13******** *****@corp.supe rnews.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.comwrot e in message
news:13******** *****@corp.supe rnews.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 (CheckIfRecordE xists())
{
ClientScript.Re gisterStartupSc ript(GetType(), "recordExis ts",
"alert('Rec ord already exists!'););
}
else
{
InsertRecord();
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Sep 6 '07 #3

"Mark Rae [MVP]" <ma**@markNOSPA Mrae.netwrote in message
news:O$******** *****@TK2MSFTNG P02.phx.gbl...
"Shelly" <sh************ @asap-consult.comwrot e in message
news:13******** *****@corp.supe rnews.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 (CheckIfRecordE xists())
{
ClientScript.Re gisterStartupSc ript(GetType(), "recordExis ts",
"alert('Rec ord 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.comwrot e in message
news:13******** *****@corp.supe rnews.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.comwrot e in message
news:13******** *****@corp.supe rnews.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.comwrot e in message
news:13******** *****@corp.supe rnews.com...
>
"Mark Rae [MVP]" <ma**@markNOSPA Mrae.netwrote in message
news:O$******** *****@TK2MSFTNG P02.phx.gbl...
>"Shelly" <sh************ @asap-consult.comwrot e in message
news:13******* ******@corp.sup ernews.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
2857
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 return code (not generating error/exception) so it is more compatible with other programming language.
6
2330
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 assemblies are loaded before shutting down. In one case, some of my DB-accessing code didn't handle a NULL value properly. But try...catch wouldn't...
7
5980
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2742
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 exception handling mechanism which will allow me to pass character information about an error and its location from lower-level classes. Can you...
2
2588
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 Layer. My questions is where should I put exception handling: 1) Should it be put in all significant methods in all layers? 2) Should I create an...
9
2530
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 the way of CPU usage), compared to the "normal" practise returning values. How true is this? Will using using exception handling, in general, be...
44
4198
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 user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box,...
4
5121
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 .NET 2003\Vc7\include\xstring(1453) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
41
3039
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 handle all of them. I don't care about which one happened, except to write out exception.Message to a log file. It seems verbose to write out...
1
3096
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
7918
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8206
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. ...
0
8220
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...
0
6621
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...
1
5713
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...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1185
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...

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.