<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:16px"><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr">You'll actually find a fair amount of disagreement over how to handle exceptions. In fact, some argue to <i>never</i> handle them and instead let them bubble up to the top on the theory that they'll show up in your error logs and you'll have no choice but to deal with them. The alternative is having exceptions get swallowed, hiding real errors, and having your app do strange things. Here's a classic anti-pattern from Java from a dev who's in a hurry:</div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><br></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr" class="" style=""><font face="Courier New, courier, monaco, monospace, sans-serif">    try {</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr" class="" style=""><font face="Courier New, courier, monaco, monospace, sans-serif">        CopyFiles();</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr" class="" style=""><font face="Courier New, courier, monaco, monospace, sans-serif">        MakeRegistryEntries();</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr" class="" style=""><font face="Courier New, courier, monaco, monospace, sans-serif">    }</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr" class="" style=""><font face="Courier New, courier, monaco, monospace, sans-serif" id="yui_3_16_0_1_1416428768427_55858">    catch (CException & e) {</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr" class="" style=""><font face="Courier New, courier, monaco, monospace, sans-serif" id="yui_3_16_0_1_1416428768427_55862">        // I'll come back to this later</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr" class="" style=""><font face="Courier New, courier, monaco, monospace, sans-serif">    }</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><br></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr">Because exceptions must often be trapped at compile-time in Java, you'll often find bad java code with empty catch blocks "swallowing" exceptions on the theory that they have a lot of work to do and will come back to it later. Later, of course, is always later. The app fails mysteriously when other devs are trying to why the copied files are intermittently not copied.</div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><br></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr">In Perl, we don't have "checked exceptions" like we do in Java, but here's how it manifests:</div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><br></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><font face="Courier New, courier, monaco, monospace, sans-serif">    try {</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><font face="Courier New, courier, monaco, monospace, sans-serif">        copy_files();</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><font face="Courier New, courier, monaco, monospace, sans-serif">        make_registry_entries();</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><font face="Courier New, courier, monaco, monospace, sans-serif">    }</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><font face="Courier New, courier, monaco, monospace, sans-serif" id="yui_3_16_0_1_1416428768427_55895">    catch {</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><font face="Courier New, courier, monaco, monospace, sans-serif" id="yui_3_16_0_1_1416428768427_55896">        my $error = $_;</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><font face="Courier New, courier, monaco, monospace, sans-serif" id="yui_3_16_0_1_1416428768427_55894">        $self->log->error($error);</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><font face="Courier New, courier, monaco, monospace, sans-serif">    };</font></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><br></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr">Well, great. We've logged the damned error, but the software didn't see it and many people don't look at the logs. By never trapping errors, they become big <b id="yui_3_16_0_1_1416428768427_55897">IN YOUR FACE</b> errors and are pretty hard to ignore.</div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><br></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr">Moving away from that point of view, there are fundamentally two types of exceptions: those you can recover from and those you cannot. For many Web-based applications, failure to connect to the database is an unrecoverable error. Don't trap it: let it bubble up and make sure your user gets a really pretty 500 page (<i id="yui_3_16_0_1_1416428768427_56133">not</i> the development 500 page with a stack trace. Major security hole!)</div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"><br></div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr">For recoverable errors (particularly low-value ones), such as failing to fetch a funny quote from the <a href="http://en.wikipedia.org/wiki/Fortune_(Unix)" id="yui_3_16_0_1_1416428768427_54573">fortune</a> command, I would trap it at the source, log the error, and make sure my app is coded in such a way that it can handle the lack of "fortune" output (perhaps by providing a default "fortune"). </div><div id="yui_3_16_0_1_1416428768427_53967" dir="ltr"> <br></div><div id="yui_3_16_0_1_1416428768427_54456" dir="ltr">Best,</div><div id="yui_3_16_0_1_1416428768427_54456" dir="ltr">Ovid</div><div id="yui_3_16_0_1_1416428768427_54455"><div id="yui_3_16_0_1_1416428768427_54454">--<br>IT consulting, training, international recruiting<br>       http://www.allaroundtheworld.fr/.<br>Buy my book! - http://bit.ly/beginning_perl<br>Live and work overseas - http://www.overseas-exile.com/</div></div> <div class="qtdSeparateBR"><br><br></div><div class="yahoo_quoted" style="display: block;"> <div style="font-family: times new roman, new york, times, serif; font-size: 16px;"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, Sans-Serif; font-size: 16px;"> <div dir="ltr"> <font size="2" face="Arial"> On Thursday, 20 November 2014, 12:29, WK <wanradt@gmail.com> wrote:<br> </font> </div> <blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; margin-top: 5px; padding-left: 5px;">  <br><br> <div class="y_msg_container"><div id="yiv7825805697"><div dir="ltr">Hi!<div><br></div><div>I have no real experience using exceptions in my code. Neither I have not seen good guide how to use exceptions in software developent. I have lot of small pieces about it, but no big picture. So I have simple question: how and where to catch errors in Dancer app? </div><div><br></div><div>I looked to Dancer::Error and it seems good, but still I don't get, where is the best place to catch and handle errors? Is it good idea to have one simple dispatcher (like in bin/<a rel="nofollow" target="_blank" href="http://app.pl/">app.pl</a>), so every exception is falling to the base script and handled there? Or is better to catch them in place all over the code? Or? Is there some "best practice"? <br clear="all"><div><br></div>-- <br><div class="yiv7825805697gmail_signature">Wbr,</div><div class="yiv7825805697gmail_signature">Kõike hääd,<br><br>Gunnar</div>
</div></div></div><br>_______________________________________________<br>dancer-users mailing list<br><a ymailto="mailto:dancer-users@dancer.pm" href="mailto:dancer-users@dancer.pm">dancer-users@dancer.pm</a><br><a href="http://lists.preshweb.co.uk/mailman/listinfo/dancer-users" target="_blank">http://lists.preshweb.co.uk/mailman/listinfo/dancer-users</a><br><br><br></div> </blockquote>  </div> </div>   </div> </div></body></html>