VMI,
You actually don't have to catch everything in the lower levels.
try
{
}
finally
{
}
//no catch
Do a google search for "try catch finally Brad Abrams" and you'll find some
tidbits about this.
In a web environment, here is what I do.
In the DataLayer, I just write try/finally's
public class EmpData
{
public void UpdateEmployee(DataSet ds)
{
try
{
//code up and run some stored procedure
}
finally
{
//clean up resource use
}
}
}
In the BusinessLayer, I ~sometimes catch the exception, and create my own
"more friendlier" exception
try
{
}
catch (System.Data.SqlException)
{
throw new MyCustomException("Houston, the database is experiencing
some issues." , ex);
}
catch (Exception ex)
{
throw new MyCustomException("Houston, this is a friendly error
regarding having a problem." , ex);
}
Creating or subclassing your own Exception is not hard. I usually have a
constructor which takes another exception....
In the webpage, in the code behind .... I have this
try
{
''do something
}
catch (Exception ex)
{
//Maybe a response.write OR a direct to a common exception showing
page
Response.Write (ex.Message);
if(null!=ex.InnerException)
{
Response.Write (ex.InnerException.Message);
}
}
The reason you might write a MyCustomException ... is to push up a more
"friendlier" Message to the user.
Like, if the database fails on a constraint violation, you can intercept
that, and give a more "The age of the person violates a database constraint"
or something like that.
However, if Presentation layer means "presentation", perhaps you could catch
the exception there, and re-word it.
I prefer the biz layer.
I actually write many many more
try/finally
instead of
try
catch (Exception ex)
{
throw;
}
finally
{
}
If you're not going to do anything but catch it, then rethrow it, then don't
waste the effort. Just don't write in a "catch" in the try/finally
PS
If you do catch an exception remember
catch(Exception ex)
{
throw ex;//erases the StackTrace
}
VS
catch(Exception ex)
{
throw;//retains the StackTrace
}
You can test it, but the first one erases the StackTrace.
The second one retains the StackTrace.
That's a nice little nugget to be aware of.
PPS
The Exception Publishing Application Block is a nice add in.
I've written a custom publisher which
Writes out an error log
Sends an email.
Updates a database.
http://www.microsoft.com/downloads/...FamilyID=8CA8EB6E-6F4A-43DF-ADEB-8F22CA173E02
The built in , default functionality is to write to the EventLog.
The Enterprise Library has an updated version, but I still find the this
version of the EMAB useable.