Re: Error Handling in a DAL

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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:uXa28ruNGHA.1032@xxxxxxxxxxxxxxxxxxxxxxx
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" <nobody@xxxxxxxxxxx> wrote in message
news:eBqZ4bmNGHA.3888@xxxxxxxxxxxxxxxxxxxxxxx
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:OLEG1WkNGHA.532@xxxxxxxxxxxxxxxxxxxxxxx
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" <nobody@xxxxxxxxxxx> wrote in message
news:ODvjtLkNGHA.3908@xxxxxxxxxxxxxxxxxxxxxxx
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.GetStoredProcCommand(sqlCommand);

try
{
db.ExecuteNonQuery(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.Insert();
if (ErrorText.ToString() != "success")
{
Response.Write (ErrorText);
}










.



Relevant Pages

  • Re: Oh boy, how did we miss this...
    ... >This error seems to trigger SEH immediately, but no handler responded because I ... use the OS's own reporting mechanism in a way that was never intended (at ... There are several different error handling mechanisms in Windows... ... Structured Exceptions ...
    (comp.lang.pascal.delphi.misc)
  • Re: Error handling in VB6
    ... The VB syntax for catching and handling errors is definitely not ... exceptions thrown in Main, and in anything called from Main. ... OnError GoTo SomeErrHandler ... VB6 has no such thing as a global error handler. ...
    (microsoft.public.vb.general.discussion)
  • Re: Error Handling in a DAL
    ... "Error handling you put somewhere else. ... handler around your main message pump, and you just handle exceptions as ... string ErrorText; ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: File Descriptors
    ... At least one Tcl ... I think that depends somewhat one where you are coming to tcl/tk from. ... With regard to error handling in particular, ... Conditions under which exceptions occur ...
    (comp.lang.tcl)
  • Re: Exception as the primary error handling mechanism?
    ... handling rather than use error code. ... In the article API Design Matters by Michi Henning ... handling exceptions is almost always slower than testing a return ... could some python expert explain to me why exception is ...
    (comp.lang.python)