The source content for blog.juliobiason.me
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
5.4 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!-- Enable responsiveness on mobile devices-->
<!-- viewport-fit=cover is to support iPhone X rounded corners and notch in landscape-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, viewport-fit=cover">
<title>Julio Biason .Me 4.3</title>
<!-- CSS -->
<link rel="stylesheet" href="https://blog.juliobiason.me/print.css" media="print">
<link rel="stylesheet" href="https://blog.juliobiason.me/poole.css">
<link rel="stylesheet" href="https://blog.juliobiason.me/hyde.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=PT+Sans:400,400italic,700|Abril+Fatface">
</head>
<body class=" ">
<div class="sidebar">
<div class="container sidebar-sticky">
<div class="sidebar-about">
<a href="https:&#x2F;&#x2F;blog.juliobiason.me"><h1>Julio Biason .Me 4.3</h1></a>
<p class="lead">Old school dev living in a 2.0 dev world</p>
</div>
<ul class="sidebar-nav">
<li class="sidebar-nav-item"><a href="&#x2F;">English</a></li>
<li class="sidebar-nav-item"><a href="&#x2F;pt">Português</a></li>
<li class="sidebar-nav-item"><a href="&#x2F;tags">Tags (EN)</a></li>
<li class="sidebar-nav-item"><a href="&#x2F;pt&#x2F;tags">Tags (PT)</a></li>
</ul>
</div>
</div>
<div class="content container">
<div class="post">
<h1 class="post-title">Things I Learnt The Hard Way - It&#x27;s Better To Let The Application Crash Than Do Nothing</h1>
<span class="post-date">
2019-06-24
<a href="https://blog.juliobiason.me/tags/books/">#books</a>
<a href="https://blog.juliobiason.me/tags/things-i-learnt/">#things i learnt</a>
<a href="https://blog.juliobiason.me/tags/exceptions/">#exceptions</a>
<a href="https://blog.juliobiason.me/tags/error-handling/">#error handling</a>
</span>
<p>Although that sounds weird, it's better to not add any error handling than
silently capturing errors and doing nothing.</p>
<span id="continue-reading"></span>
<p>For example, a (sadly common) example of Java code:</p>
<pre data-lang="java" style="background-color:#2b303b;color:#c0c5ce;" class="language-java "><code class="language-java" data-lang="java"><span style="color:#b48ead;">try </span><span>{
</span><span> </span><span style="color:#bf616a;">something_that_can_raise_exception</span><span>()
</span><span>} </span><span style="color:#b48ead;">catch </span><span>(</span><span style="color:#ebcb8b;">Exception </span><span style="color:#bf616a;">ex</span><span>) {
</span><span> </span><span style="color:#ebcb8b;">System</span><span>.out.</span><span style="color:#bf616a;">println</span><span>(ex);
</span><span>}
</span></code></pre>
<p>This does nothing to deal with the exception -- besides printing it, that is.</p>
<p>The example may be a bit bad, 'cause Java forces capturing exceptions on
functions that throw exceptions and it forces functions to mark themselves as
throwing exceptions if there a <code>throw</code> in them.</p>
<p>But Python doesn't have this restriction and people <em>still</em> try to capture
exceptions for doing absolutely nothing -- and, worse, just keep the execution
going.</p>
<p>If the language allows it, you should let the application crash due the lack
of error handling -- as long as you don't have any idea on how to handle it.
Then, when they crash, you can think of a way to deal with it, instead of
silently capturing it and doing nothing.</p>
<p>Also, keep in mind to not go forth and capture <em>every</em> exception/error in a
single take -- like the example above, which will capture every exception, or
like <code>except Exception</code> in Python. This last example actually happened to me
when another developer added this &quot;broad except&quot;<sup class="footnote-reference"><a href="#1">1</a></sup> in a network code and, at
some point, the code would get into the capture all the time. We checked every
cable, every connection, every interface, till I noticed there was a syntax
error in the code. In Python, syntax errors raise exceptions and, because we
had a &quot;capture all exceptions&quot;, we lost some pretty good time looking for the
problem in the wrong place.</p>
<div class="footnote-definition" id="1"><sup class="footnote-definition-label">1</sup>
<p>As called by Pylint.</p>
</div>
<div>
<div style="float:left">
&lt;&lt; <a href="&#x2F;books&#x2F;things-i-learnt&#x2F;interface-changes">Beware of Interface Changes</a>
</div>
&nbsp;
<div style="float:right">
<a href="&#x2F;books&#x2F;things-i-learnt&#x2F;handle-it">If You Know How To Handle It, Handle It</a> &gt;&gt;
</div>
</div>
</div>
</div>
</body>
</html>