I'm trying to create a DAL and am wondering what's the proper way to
handle errors in this Insert method.
public string Insert()
{
Database db = DatabaseFactory .CreateDatabase ();
string sqlCommand = "pr_testx";
DbCommand dbCommand = db.GetStoredPro cCommand(sqlCom mand);
try
{
db.ExecuteNonQu ery(dbCommand);
}
catch (SqlException SqlExc)
{
return " ERROR:" + SqlExc.Message + "<br>";
}
catch (Exception ex)
{
return " ERROR:" + ex.Message + "<br>";
}
return "success";
}
I call it with the following and it works (it's intentionally generating a proc
not found error), but I'm not sure this is the best way to implement error
handling.
string ErrorText;
ErrorText = saleProduct.Ins ert();
if (ErrorText.ToSt ring() != "success")
{
Response.Write (ErrorText);
} 7 2332
Your error handling isn't very good...you are CATCHING the error but not
HANDLING it...instead you are swallowing any possible error.
90% of the time you can't really handle an error, all you can do is clean up
after yourself, that's why the using keyword exists. The proper way to do
this is:
using (connection...) {
using(command){
using (datareader{
execute
}
}
}
and then in your global.asax's OnError, catch any unhandled error, log it
(if desired) and display a frienly error message.
Karl
-- http://www.openmymind.net/ http://www.fuelindustries.com/
"Garth Wells" <no****@nowhere .com> wrote in message
news:OD******** ******@TK2MSFTN GP10.phx.gbl... I'm trying to create a DAL and am wondering what's the proper way to handle errors in this Insert method.
public string Insert() { Database db = DatabaseFactory .CreateDatabase (); string sqlCommand = "pr_testx"; DbCommand dbCommand = db.GetStoredPro cCommand(sqlCom mand);
try { db.ExecuteNonQu ery(dbCommand); } catch (SqlException SqlExc) { return " ERROR:" + SqlExc.Message + "<br>"; } catch (Exception ex) { return " ERROR:" + ex.Message + "<br>"; }
return "success"; }
I call it with the following and it works (it's intentionally generating a proc not found error), but I'm not sure this is the best way to implement error handling.
string ErrorText; ErrorText = saleProduct.Ins ert(); if (ErrorText.ToSt ring() != "success") { Response.Write (ErrorText); }
"Garth Wells" <no****@nowhere .com> wrote in message
news:OD******** ******@TK2MSFTN GP10.phx.gbl... I'm trying to create a DAL and am wondering what's the proper way to handle errors in this Insert method.
It doesn't really look as if you're handling the error(s) at all - other
than catching them, what are you actually doing about them...? What are you
doing to prevent unclosed connections? How are you destroying any object
variables? Consider creating objects with the "using" reserved word, or
implementing a finally{...} clause...
Thanks for the feedback. Can you point me to a full example of this on the
web? I didn't find any that used this exact approach.
Thanks
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:OL******** *****@TK2MSFTNG P15.phx.gbl... Your error handling isn't very good...you are CATCHING the error but not HANDLING it...instead you are swallowing any possible error.
90% of the time you can't really handle an error, all you can do is clean up after yourself, that's why the using keyword exists. The proper way to do this is:
using (connection...) { using(command){ using (datareader{ execute } } }
and then in your global.asax's OnError, catch any unhandled error, log it (if desired) and display a frienly error message.
Karl
-- http://www.openmymind.net/ http://www.fuelindustries.com/
"Garth Wells" <no****@nowhere .com> wrote in message news:OD******** ******@TK2MSFTN GP10.phx.gbl... I'm trying to create a DAL and am wondering what's the proper way to handle errors in this Insert method.
public string Insert() { Database db = DatabaseFactory .CreateDatabase (); string sqlCommand = "pr_testx"; DbCommand dbCommand = db.GetStoredPro cCommand(sqlCom mand);
try { db.ExecuteNonQu ery(dbCommand); } catch (SqlException SqlExc) { return " ERROR:" + SqlExc.Message + "<br>"; } catch (Exception ex) { return " ERROR:" + ex.Message + "<br>"; }
return "success"; }
I call it with the following and it works (it's intentionally generating a proc not found error), but I'm not sure this is the best way to implement error handling.
string ErrorText; ErrorText = saleProduct.Ins ert(); if (ErrorText.ToSt ring() != "success") { Response.Write (ErrorText); }
I don't know of any off-hand. It isn't really an "approach", it's
There's a great interview with Anders (distinguished engineer, guy in charge
of c#) about checked exceptions. http://www.artima.com/intv/handcuffs2.html
I liked you to page 2, you'll find the last 3 sections where he talks
informative, but most relevant:
"Error handling you put somewhere else. Surely in any kind of event-driven
application like any kind of modern UI, you typically put an exception
handler around your main message pump, and you just handle exceptions as
they fall out that way. But you make sure you protect yourself all the way
out by deallocating any resources you've grabbed, and so forth. You clean up
after yourself, so you're always in a consistent state. You don't want a
program where in 100 different places you handle exceptions and pop up error
dialogs. What if you want to change the way you put up that dialog box?
That's just terrible. The exception handling should be centralized, and you
should just protect yourself as the exceptions propagate out to the
handler."
Karl
-- http://www.openmymind.net/ http://www.fuelindustries.com/
"Garth Wells" <no****@nowhere .com> wrote in message
news:eB******** ******@TK2MSFTN GP12.phx.gbl... Thanks for the feedback. Can you point me to a full example of this on the web? I didn't find any that used this exact approach.
Thanks
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:OL******** *****@TK2MSFTNG P15.phx.gbl... Your error handling isn't very good...you are CATCHING the error but not HANDLING it...instead you are swallowing any possible error.
90% of the time you can't really handle an error, all you can do is clean up after yourself, that's why the using keyword exists. The proper way to do this is:
using (connection...) { using(command){ using (datareader{ execute } } }
and then in your global.asax's OnError, catch any unhandled error, log it (if desired) and display a frienly error message.
Karl
-- http://www.openmymind.net/ http://www.fuelindustries.com/
"Garth Wells" <no****@nowhere .com> wrote in message news:OD******** ******@TK2MSFTN GP10.phx.gbl... > I'm trying to create a DAL and am wondering what's the proper way to > handle errors in this Insert method. > > > public string Insert() > { > Database db = DatabaseFactory .CreateDatabase (); > string sqlCommand = "pr_testx"; > DbCommand dbCommand = db.GetStoredPro cCommand(sqlCom mand); > > try > { > db.ExecuteNonQu ery(dbCommand); > } > catch (SqlException SqlExc) > { > return " ERROR:" + SqlExc.Message + "<br>"; > } > catch (Exception ex) > { > return " ERROR:" + ex.Message + "<br>"; > } > > return "success"; > } > > > I call it with the following and it works (it's intentionally > generating a > proc > not found error), but I'm not sure this is the best way to implement > error > handling. > > string ErrorText; > ErrorText = saleProduct.Ins ert(); > if (ErrorText.ToSt ring() != "success") > { > Response.Write (ErrorText); > } > >
Thanks
Interesting article...too bad he didn't tell the people who write the MSDN
examples about this approach. Found this in the: "How to: Handle
Application-Level Errors" topic.
"It is preferable to use Try/Catch blocks around any code that is subject to
errors rather than rely on a global error handler."
They sure do make it hard to adopt a best practice...
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:uX******** ******@TK2MSFTN GP11.phx.gbl... I don't know of any off-hand. It isn't really an "approach", it's
There's a great interview with Anders (distinguished engineer, guy in charge of c#) about checked exceptions.
http://www.artima.com/intv/handcuffs2.html
I liked you to page 2, you'll find the last 3 sections where he talks informative, but most relevant:
"Error handling you put somewhere else. Surely in any kind of event-driven application like any kind of modern UI, you typically put an exception handler around your main message pump, and you just handle exceptions as they fall out that way. But you make sure you protect yourself all the way out by deallocating any resources you've grabbed, and so forth. You clean up after yourself, so you're always in a consistent state. You don't want a program where in 100 different places you handle exceptions and pop up error dialogs. What if you want to change the way you put up that dialog box? That's just terrible. The exception handling should be centralized, and you should just protect yourself as the exceptions propagate out to the handler."
Karl -- http://www.openmymind.net/ http://www.fuelindustries.com/
"Garth Wells" <no****@nowhere .com> wrote in message news:eB******** ******@TK2MSFTN GP12.phx.gbl... Thanks for the feedback. Can you point me to a full example of this on the web? I didn't find any that used this exact approach.
Thanks
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:OL******** *****@TK2MSFTNG P15.phx.gbl... Your error handling isn't very good...you are CATCHING the error but not HANDLING it...instead you are swallowing any possible error.
90% of the time you can't really handle an error, all you can do is clean up after yourself, that's why the using keyword exists. The proper way to do this is:
using (connection...) { using(command){ using (datareader{ execute } } }
and then in your global.asax's OnError, catch any unhandled error, log it (if desired) and display a frienly error message.
Karl
-- http://www.openmymind.net/ http://www.fuelindustries.com/
"Garth Wells" <no****@nowhere .com> wrote in message news:OD******** ******@TK2MSFTN GP10.phx.gbl... > I'm trying to create a DAL and am wondering what's the proper way to > handle errors in this Insert method. > > > public string Insert() > { > Database db = DatabaseFactory .CreateDatabase (); > string sqlCommand = "pr_testx"; > DbCommand dbCommand = db.GetStoredPro cCommand(sqlCom mand); > > try > { > db.ExecuteNonQu ery(dbCommand); > } > catch (SqlException SqlExc) > { > return " ERROR:" + SqlExc.Message + "<br>"; > } > catch (Exception ex) > { > return " ERROR:" + ex.Message + "<br>"; > } > > return "success"; > } > > > I call it with the following and it works (it's intentionally > generating a > proc > not found error), but I'm not sure this is the best way to implement > error > handling. > > string ErrorText; > ErrorText = saleProduct.Ins ert(); > if (ErrorText.ToSt ring() != "success") > { > Response.Write (ErrorText); > } > >
Can you provide me with a specific link? I'll see what i can do :)
Karl
-- http://www.openmymind.net/ http://www.fuelindustries.com/
"Garth Wells" <no****@nowhere .com> wrote in message
news:eQ******** *****@tk2msftng p13.phx.gbl... Thanks
Interesting article...too bad he didn't tell the people who write the MSDN examples about this approach. Found this in the: "How to: Handle Application-Level Errors" topic.
"It is preferable to use Try/Catch blocks around any code that is subject to errors rather than rely on a global error handler."
They sure do make it hard to adopt a best practice...
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:uX******** ******@TK2MSFTN GP11.phx.gbl... I don't know of any off-hand. It isn't really an "approach", it's
There's a great interview with Anders (distinguished engineer, guy in charge of c#) about checked exceptions.
http://www.artima.com/intv/handcuffs2.html
I liked you to page 2, you'll find the last 3 sections where he talks informative, but most relevant:
"Error handling you put somewhere else. Surely in any kind of event-driven application like any kind of modern UI, you typically put an exception handler around your main message pump, and you just handle exceptions as they fall out that way. But you make sure you protect yourself all the way out by deallocating any resources you've grabbed, and so forth. You clean up after yourself, so you're always in a consistent state. You don't want a program where in 100 different places you handle exceptions and pop up error dialogs. What if you want to change the way you put up that dialog box? That's just terrible. The exception handling should be centralized, and you should just protect yourself as the exceptions propagate out to the handler."
Karl -- http://www.openmymind.net/ http://www.fuelindustries.com/
"Garth Wells" <no****@nowhere .com> wrote in message news:eB******** ******@TK2MSFTN GP12.phx.gbl... > Thanks for the feedback. Can you point me to a full example of this on > the > web? I didn't find any that used this exact approach. > > Thanks > > > "Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . > ANDME > net> > wrote in message news:OL******** *****@TK2MSFTNG P15.phx.gbl... >> Your error handling isn't very good...you are CATCHING the error but >> not >> HANDLING it...instead you are swallowing any possible error. >> >> 90% of the time you can't really handle an error, all you can do is >> clean >> up >> after yourself, that's why the using keyword exists. The proper way to >> do >> this is: >> >> using (connection...) { >> using(command){ >> using (datareader{ >> execute >> } >> } >> } >> >> and then in your global.asax's OnError, catch any unhandled error, log >> it >> (if desired) and display a frienly error message. >> >> Karl >> >> -- >> http://www.openmymind.net/ >> http://www.fuelindustries.com/ >> >> >> "Garth Wells" <no****@nowhere .com> wrote in message >> news:OD******** ******@TK2MSFTN GP10.phx.gbl... >> > I'm trying to create a DAL and am wondering what's the proper way to >> > handle errors in this Insert method. >> > >> > >> > public string Insert() >> > { >> > Database db = DatabaseFactory .CreateDatabase (); >> > string sqlCommand = "pr_testx"; >> > DbCommand dbCommand = db.GetStoredPro cCommand(sqlCom mand); >> > >> > try >> > { >> > db.ExecuteNonQu ery(dbCommand); >> > } >> > catch (SqlException SqlExc) >> > { >> > return " ERROR:" + SqlExc.Message + "<br>"; >> > } >> > catch (Exception ex) >> > { >> > return " ERROR:" + ex.Message + "<br>"; >> > } >> > >> > return "success"; >> > } >> > >> > >> > I call it with the following and it works (it's intentionally >> > generating a >> > proc >> > not found error), but I'm not sure this is the best way to implement >> > error >> > handling. >> > >> > string ErrorText; >> > ErrorText = saleProduct.Ins ert(); >> > if (ErrorText.ToSt ring() != "success") >> > { >> > Response.Write (ErrorText); >> > } >> > >> > >> >> > >
http://msdn2.microsoft.com/en-us/library/24395wz3.aspx
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:#S******** ******@TK2MSFTN GP15.phx.gbl... Can you provide me with a specific link? I'll see what i can do :)
Karl -- http://www.openmymind.net/ http://www.fuelindustries.com/
"Garth Wells" <no****@nowhere .com> wrote in message news:eQ******** *****@tk2msftng p13.phx.gbl... Thanks
Interesting article...too bad he didn't tell the people who write the MSDN examples about this approach. Found this in the: "How to: Handle Application-Level Errors" topic.
"It is preferable to use Try/Catch blocks around any code that is subject to errors rather than rely on a global error handler."
They sure do make it hard to adopt a best practice...
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:uX******** ******@TK2MSFTN GP11.phx.gbl... I don't know of any off-hand. It isn't really an "approach", it's
There's a great interview with Anders (distinguished engineer, guy in charge of c#) about checked exceptions.
http://www.artima.com/intv/handcuffs2.html
I liked you to page 2, you'll find the last 3 sections where he talks informative, but most relevant:
"Error handling you put somewhere else. Surely in any kind of event-driven application like any kind of modern UI, you typically put an exception handler around your main message pump, and you just handle exceptions as they fall out that way. But you make sure you protect yourself all the way out by deallocating any resources you've grabbed, and so forth. You clean up after yourself, so you're always in a consistent state. You don't want a program where in 100 different places you handle exceptions and pop up error dialogs. What if you want to change the way you put up that dialog box? That's just terrible. The exception handling should be centralized, and you should just protect yourself as the exceptions propagate out to the handler."
Karl -- http://www.openmymind.net/ http://www.fuelindustries.com/
"Garth Wells" <no****@nowhere .com> wrote in message news:eB******** ******@TK2MSFTN GP12.phx.gbl... > Thanks for the feedback. Can you point me to a full example of this on > the > web? I didn't find any that used this exact approach. > > Thanks > > > "Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . > ANDME > net> > wrote in message news:OL******** *****@TK2MSFTNG P15.phx.gbl... >> Your error handling isn't very good...you are CATCHING the error but >> not >> HANDLING it...instead you are swallowing any possible error. >> >> 90% of the time you can't really handle an error, all you can do is >> clean >> up >> after yourself, that's why the using keyword exists. The proper way to >> do >> this is: >> >> using (connection...) { >> using(command){ >> using (datareader{ >> execute >> } >> } >> } >> >> and then in your global.asax's OnError, catch any unhandled error, log >> it >> (if desired) and display a frienly error message. >> >> Karl >> >> -- >> http://www.openmymind.net/ >> http://www.fuelindustries.com/ >> >> >> "Garth Wells" <no****@nowhere .com> wrote in message >> news:OD******** ******@TK2MSFTN GP10.phx.gbl... >> > I'm trying to create a DAL and am wondering what's the proper way to >> > handle errors in this Insert method. >> > >> > >> > public string Insert() >> > { >> > Database db = DatabaseFactory .CreateDatabase (); >> > string sqlCommand = "pr_testx"; >> > DbCommand dbCommand = db.GetStoredPro cCommand(sqlCom mand); >> > >> > try >> > { >> > db.ExecuteNonQu ery(dbCommand); >> > } >> > catch (SqlException SqlExc) >> > { >> > return " ERROR:" + SqlExc.Message + "<br>"; >> > } >> > catch (Exception ex) >> > { >> > return " ERROR:" + ex.Message + "<br>"; >> > } >> > >> > return "success"; >> > } >> > >> > >> > I call it with the following and it works (it's intentionally >> > generating a >> > proc >> > not found error), but I'm not sure this is the best way to implement >> > error >> > handling. >> > >> > string ErrorText; >> > ErrorText = saleProduct.Ins ert(); >> > if (ErrorText.ToSt ring() != "success") >> > { >> > Response.Write (ErrorText); >> > } >> > >> > >> >> > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: WSeeger |
last post by:
When creating a new class, is it encouraged to always include error
handling routines within your LET and GET procedures? It's seems that
most text books never seem to include much about error handling within
classes. Just hoping to hear some programmer's thoughts on error
handling.
|
by: Christian Christmann |
last post by:
Hi,
assert and error handling can be used for similar purposes.
When should one use assert instead of try/catch and in which
cases the error handling is preferable?
I've read somewhere that assert could be used to start
an interactive debugger automatically. How do I realize that
on a Linux machine using gcc?
|
by: Squirrel |
last post by:
I have a command button on a subform to delete a record.
The only statement in the subroutine is:
DoCmd.RunCommand acCmdDeleteRecord
The subform's recordsource is "select * from tblVisit order by VisitDt"
I'm getting this error message:
Errno is 2465. Err.description is "Can't find field '|' referred to in your
expression"
|
by: Thelma Lubkin |
last post by:
I use code extensively; I probably overuse it. But I've been using
error trapping very sparingly, and now I've been trapped by that.
A form that works for me on the system I'm using, apparently runs into
problems on the system where it will actually be used, and since I
used so little error-trapping it dies very ungracefully.
I will of...
|
by: Anthony England |
last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled
error is encountered, but it seems that this also happens when the variable
is defined as private at form-level.
So if "global variables get re-set" doesn't tell the whole story, then what
does?
***please note***
I'm not looking for a solution - I'm looking for a...
| |
by: Stefan Johansson |
last post by:
Hi all
I'am moving from Visual Foxpro and have a question
regarding "best practice" error handling in vb .net.
In VFP I have always used a "central" error handling
object in order to have a easy and reusable way of
handling all errors in a program.
The VB 6 coding examples I have seen there has always
been error handling code in each...
|
by: Al Williams |
last post by:
Hi,
I have error handling in place throughout my application. I also start the
application wrapped in error handling code to catch any unexpected
exceptions (i.e. exceptions that occur where I haven't placed error handling
code).
When I run my app from the IDE, the unhandled errors are caught by the error
handling code in my Sub Main...
|
by: Anthony England |
last post by:
(sorry for the likely repost, but it is still not showing on my news server
and after that much typing, I don't want to lose it)
I am considering general error handling routines and have written a sample
function to look up an ID in a table. The function returns True if it can
find the ID and create a recordset based on that ID, otherwise...
|
by: Lysander |
last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access Security, but I'll start with something short and simple
This code was written in Access 2003 but should be valid in Access 2000
By default, when you...
|
by: MrDeej |
last post by:
Hello guys!
We have an SQL server which sometimes makes timeouts and connection errors. And we have an function witch writes and updates data in 2 tables on this server. When the SQL server error appears it, in 99%, of the cases, works if we just press the play button in VBA debug. Therefor we have maked an error handling which just tryes...
|
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...
| |
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...
|
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. ...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |