[Vtigercrm-developers] Day 4: Error handling

Holbok István holbok at gmail.com
Mon Dec 5 17:07:47 GMT 2016


Today, in day 4 of our 5-day mini-course, we're going to discuss *Error
handling*.



    Error handling

For a long while, error handling was just about magical status codes for
us. You know what I am talking about, right?

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.

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.

Error codes are terrible; they are meaningless to new developers; are
hard to keep track of because they contain no real information.

Error handling should be done properly with exceptions.

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.

PHP introduced Exception class in PHP 5.

Similar to exceptions in other languages, an exception is "thrown" when
an error occurs:

throw new Exception('No more stock')

And an Exception class ought to be handled in a catch clause:

catch (Exception $e) {
     echo 'Caught exception: ',  $e->getMessage(), "\n";
}

To make sure some code always runs such as closing a database
connection, we use a finally clause:

finally {
     // close PDO connection
}

Now you know error handling does not mean returning magical codes. Using
exceptions to handle exceptional cases is the way to go!

Tomorrow, in the final installment of this course, we'll take a look
at *Test Driven Development.*

You won't want to miss it,

Keep learning!
Xu
www.startutorial.com

 
Kindest regards:
Istvan Holbok
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20161205/9c70bd92/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: open
Type: image/gif
Size: 43 bytes
Desc: not available
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20161205/9c70bd92/attachment.gif>


More information about the vtigercrm-developers mailing list