First I would like to start by a quote I’ve learnt from Dr.yasser farouk (one of the doctor I’m proud to learned from) ‘as a developer it’s a shame to let the OS throw your code out of CPU because of division by zero or array out of bounds’

He was right about that and it’s also a good behavior and makes It easy for the developer to eliminate bugs. For example: you’re trying to persist a user object in database which has no ’email’ property set(which is required by your class responsible for db operations). This will throw an exception says that ’email’ property should not be null and then you should have an if statement to check if the required properties are not nulls (which is a really big hassle for an object with 20 property for example).

For me as a web developer (who writes in php) exception handling saves me a lot of time spent of dangling if else to check every single case. Using exception handling makes it easy as putting the code which I suspect in a try block such as follows:


try {
$fbdata = $facebook->get($fbToken, $fbId);
} catch (\FacebookApiException $e) {
throw new Exception\AuthenticationFailedException('User exists but Facebook token invalid');
}

As an architect you should start designing classes to serve your custom exceptions.

As a Symfony2 lover I’ll show you an example how to do it in Symfony by extending the existing exception classes:


<?php

namespace Smartizer\EventxBundle\Service\Exception;

class AuthenticationFailedException extends LogicException
{
}

as you may notice it’s pretty easy to get started and saves you alot of time spend on checking and dangling if else.

hope you’ve liked my post and start defining your custom exceptions.

Advertisements

4 responses »

  1. I hope you know the cost of exception.

    What happen actually is “Your code gets out of the CPU and Error happen”, the only thing exception do is “It steps back for you”, the performance cost is very high.

    I personally don’t use exception except for “VERY” rare cases.

    • yes it has high cost but your comment applies in cases that have too much fails and exceptions. Also in case you have for example 30 property for your object and you’ve to check each value of them i think exception handling will have a lower cost. At the end of the day it’s a cost balance and yes it’s “Art” of using exception handling.

      • Man, you can simply do a proxy to fix the misplaced property instead of using exception of course with some schema and meta data tricks (it will not take much time)

        And again, if you are sacrificing performance for conveniency then it is your choice 🙂

      • I’m quite interested in your opinion. Specially that I’m interested to optimize whatever I do, now you win 😀

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s