Foundations of Programming - Karl Seguin
Foundations of Programming - Karl Seguin
Foundations of Programming - Karl Seguin
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
{<br />
}<br />
Chapter 8 - Back to Basics: Exceptions<br />
categoryId = 1;<br />
The problem with the above code is that regardless <strong>of</strong> the type <strong>of</strong> exception thrown, it'll be handled the<br />
same way. But does setting the categoryId to a default value <strong>of</strong> 1 actually handle an<br />
OutOfMemoryException? Instead, the above could should catch a specific exception:<br />
int categoryId;<br />
try<br />
{<br />
categoryId = int.Parse(Request.QueryString["categoryId"])<br />
}<br />
catch(FormatException)<br />
{<br />
categoryId = -1;<br />
}<br />
(an even better approach would be the use the int.TryParse function introduced in .NET 2.0 -<br />
especially considering that int.Parse can throw two other types <strong>of</strong> exceptions that we'd want to<br />
handle the same way, but that's besides the point).<br />
Logging<br />
Even though most exceptions are going to go unhandled, you should still log each and every one <strong>of</strong><br />
them. Ideally you'll centralize your logging - an HttpModule's OnError event is your best choice for an<br />
ASP.NET application or web service. I've <strong>of</strong>ten seen developers catch exceptions where they occur only<br />
to log and rethrow (more on rethrowing in a bit). This causes<br />
a lot <strong>of</strong> unnecessary and repetitive code - better to let<br />
exceptions bubble up through your code and log all<br />
exceptions at the outer edge <strong>of</strong> your system. Exactly which<br />
logging implementation you use is up to you and will depend<br />
on the criticalness <strong>of</strong> your system. Maybe you'll want to be<br />
notified by email as soon as exceptions occur, or maybe you<br />
can simply log it to a file or database and either review it<br />
daily or have another process send you a daily summary.<br />
Many developers leverage rich logging frameworks such as<br />
log4net or Micros<strong>of</strong>t's Logging Application Block.<br />
Cleaning Up<br />
In the previous chapter we talked about deterministic finalization with respect to the lazy nature <strong>of</strong> the<br />
garbage collector. Exceptions prove to be an added complexity as their abrupt nature can cause<br />
Dispose not to be called. A failed database call is a classic example:<br />
<strong>Foundations</strong> <strong>of</strong> <strong>Programming</strong> Copyright © <strong>Karl</strong> <strong>Seguin</strong> www.codebetter.com<br />
65<br />
A word <strong>of</strong> warning based on a bad<br />
personal experience: some types <strong>of</strong><br />
exceptions tend to cluster. If you<br />
choose to send out emails<br />
whenever an exception occurs you<br />
can easily flood your mail server . A<br />
smart logging solution should<br />
probably implement some type <strong>of</strong><br />
buffering or aggregation.