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.
Chapter 8 - Back to Basics: Exceptions<br />
what we want - rolling back our transaction really doesn't help anyone else handle the exception.<br />
However, there's a way to rethrow an exception which will make it look like the exception occurred<br />
within our code:<br />
catch (HibernateException ex)<br />
{<br />
if (transaction != null) { transaction.Rollback(); }<br />
throw ex;<br />
}<br />
By explicitly rethrowing the exception, the stack trace is modified so that the rethrowing line appears to<br />
be the source. This is almost always certainly a bad idea, as vital information is lost. So be careful how<br />
you rethrow exceptions - the difference is subtle but important.<br />
If you find yourself in a situation where you think you want to rethrow an exception with your handler<br />
as the source, a better approach is to use a nested exception:<br />
catch (HibernateException ex)<br />
{<br />
if (transaction != null) { transaction.Rollback(); }<br />
throw new Exception("Email already in use", ex);<br />
}<br />
This way the original stack trace is still accessible via the InnerException property exposed by all<br />
exceptions.<br />
When To Throw Exceptions<br />
It's important to know how to throw exceptions. A far more interesting topic though is when and why<br />
you should throw them. Having someone else's unruly code bring down your application is one thing.<br />
Writing your own code that'll do the same thing just seems plain silly. However, a good developer isn't<br />
afraid to judicially use exceptions.<br />
There are actually two levels <strong>of</strong> thought on how exceptions should be used. The first level, which is<br />
universally accepted, is that you shouldn't hesitate to raise an exception whenever a truly exceptional<br />
situation occurs. My favorite example is the parsing <strong>of</strong> configuration files. Many developers generously<br />
use default values for any invalid entries. This is ok in some cases, but in others it can put the system in<br />
an unreliable or unexpected state. Another example might be a Facebook application that gets an<br />
unexpected result from an API call. You could ignore the error, or you could raise an exception, log it (so<br />
that you can fix it, since the API might have changed) and present a helpful message to your users.<br />
The other belief is that exceptions shouldn't just be reserved for exceptional situations, but for any<br />
situation in which the expected behavior cannot be executed. This approach is related to the design by<br />
<strong>Foundations</strong> <strong>of</strong> <strong>Programming</strong> Copyright © <strong>Karl</strong> <strong>Seguin</strong> www.codebetter.com<br />
68