DEV Community

Saber Hosney
Saber Hosney

Posted on

Working with custom exceptions in namespaced files

Hello fellow devs !
today I came across a situation where I need to use Exceptions within a namespaced project, It was either I use the global namespace \Exception or I create a custom one. And of course we wouldn't be reading this if I choose the former :", Okay enough chat, let's dig in !

Assuming that you are familiar with composer autoload

let's create our namespaced class checker.php

<?php
namespace SHD;
class Checker
{
    public function checkPalindrome($text){
        $text = preg_replace('/[^A-Za-z0-9]/', '', $text);
        $text = strtolower($text);
        $before = $text;
        $after = strrev($text);
        if ($before == $after){
            echo "Wow, you can read this text correctly from both ways";
        } else {
            echo "This text isn't a palindrome!";
        }
    }
}
?>
Enter fullscreen mode Exit fullscreen mode

now let's try using it in index.php

<?php
//Composer autoload
require 'vendor/autoload.php';
//Loading the class and create new instance
use SHD\Checker as Checker;
$checker= new Checker;
//Performing our actions
echo $checker->checkPalindrome("A man, a plan, a canal – Panama");
?>
Enter fullscreen mode Exit fullscreen mode

So far so good, right ? lets tweak the above code to create our custom exceptionchecker.php

<?php
namespace SHD;
//Loading our custom Exception Class
use SHD\Exception;
class Checker
{
    public function checkPalindrome($text){
        //previousCode
        try {
            if ($before == $after){
                echo "Wow, you can read this text correctly from both ways";
            } else {
                throw new palindromeException("Not valid palindrome", 4);
            }
        } catch (palindromeException $e) {
            echo $e->showError();
        }
    }
}
?>
Enter fullscreen mode Exit fullscreen mode

exception.php file where the custom exceptions are!

<?php

namespace SHD;
//Loading global exception is a must before it can be extended
use Exception;
class palindromeException extends Exception
{
//This code is required to make the exception code and message not optional
    public function __construct($message, $code, Exception $previous = null) {
//Passing 'em to the Exception calss itself
        parent::__construct($message, $code, $previous);
    }
//Our custom method
    public function showError(){
        $error = $this->message;
        if ($this->code == "4") {
            $error = $this->code .":".$this->message;
        }
        return $error;
    }
}

?>
Enter fullscreen mode Exit fullscreen mode

All done, now back to index.php and change the sentence a bit to trigger the exception and watch your hard work pays off

Maybe most of you (lovely readers who reached this point of the article) already knew this, but for people like me who struggles with such easy tasks, this will be a quick reference.
Thank you all for reading <3

Top comments (0)