<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
</head>
<body style="-ms-text-size-adjust: 100%; -webkit-text-size-adjust:
100%; margin: 0; padding: 0; width: 100% !important"
bgcolor="#FFFFFF" text="#000000">
<style type="text/css">
body {
width: 100% !important; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; margin: 0; padding: 0;
}
.ExternalClass {
width: 100%;
}
.ExternalClass {
line-height: 100%;
}
#backgroundTable {
margin: 0; padding: 0; width: 600px !important; line-height: 100% !important;
}
img {
outline: none; text-decoration: none; -ms-interpolation-mode: bicubic;
}
h1 a:active {
color: red !important;
}
h2 a:active {
color: red !important;
}
h3 a:active {
color: red !important;
}
h4 a:active {
color: red !important;
}
h5 a:active {
color: red !important;
}
h6 a:active {
color: red !important;
}
h1 a:visited {
color: purple !important;
}
h2 a:visited {
color: purple !important;
}
h3 a:visited {
color: purple !important;
}
h4 a:visited {
color: purple !important;
}
h5 a:visited {
color: purple !important;
}
h6 a:visited {
color: purple !important;
}
</style>
<div id="main-content" style="color: #222222; font-family:
'Helvetica', 'Arial', sans-serif; font-size: 14px; line-height:
1.4; padding: 25px; width: 550px">Today, in day 4 of our 5-day
mini-course, we're going to discuss <strong style="font-weight:
bold">Error handling</strong>.
<h2 style="color: black !important; font-size: 16px; font-weight:
bold; margin: 0 0 12px; padding: 0">
<br>
Error handling</h2>
<p style="margin: 1em 0">For a long while, error handling was just
about magical status codes for us. You know what I am talking
about, right?</p>
<p style="margin: 1em 0">When user submits an order, return 0 when
the date is expired, return 1 when there is no more stock,
return 2 when user is not eligible for current order, return 3
when user is not logged in, and return 4 when order is placed
successfully.</p>
<p style="margin: 1em 0">Have you done anything similar above? If
so, you need to learn a better way of error handling. We did,
and we’ve never looked back.</p>
<p style="margin: 1em 0">Error codes are terrible; they are
meaningless to new developers; are hard to keep track of because
they contain no real information.</p>
<p style="margin: 1em 0">Error handling should be done properly
with exceptions.</p>
<p style="margin: 1em 0">A lot of errors in our applications are
exceptions. They are exceptional cases in our applications,
which we should handle directly as soon as they occur.</p>
<p style="margin: 1em 0">PHP introduced Exception class in PHP 5.</p>
<p style="margin: 1em 0">Similar to exceptions in other languages,
an exception is "thrown" when an error occurs:</p>
<pre>throw new Exception('No more stock')
</pre>
And an Exception class ought to be handled in a catch clause:
<pre>catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
</pre>
To make sure some code always runs such as closing a database
connection, we use a finally clause:
<pre>finally {
// close PDO connection
}
</pre>
Now you know error handling does not mean returning magical codes.
Using exceptions to handle exceptional cases is the way to go!<br>
<br>
Tomorrow, in the final installment of this course, we'll take a
look at <strong style="font-weight: bold"><span
style="line-height: 1.6">Test Driven Development</span>.</strong><br>
<br>
You won't want to miss it,<br>
<br>
Keep learning!<br>
Xu<br>
<a class="moz-txt-link-abbreviated" href="http://www.startutorial.com">www.startutorial.com</a><br>
<br>
</div>
<div id="footer" style="border-top-color: #d0d0d0; border-top-style:
solid; border-top-width: 1px; color: #777; font-family:
'Helvetica', 'Arial', sans-serif; font-size: 13px; line-height:
1.4; padding: 25px; width: 550px">Kindest regards:<br>
Istvan Holbok<br>
</div>
<img src="cid:part1.85B421AF.EAB3A2D3@gmail.com" alt=""
style="height:1px !important;width:1px !important;border-width:0
!important;margin-top:0 !important;margin-bottom:0
!important;margin-right:0 !important;margin-left:0
!important;padding-top:0 !important;padding-bottom:0
!important;padding-right:0 !important;padding-left:0 !important;"
height="1" border="0" width="1">
</body>
</html>