Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php class extends exception


<?php
/**
 * Define a custom exception class
 */
class MyException extends Exception
{
    // Redefine the exception so message isn't optional
    public function __construct($message, $code = 0, Throwable $previous = null) {
        // some code
    
        // make sure everything is assigned properly
        parent::__construct($message, $code, $previous);
    }

    // custom string representation of object
    public function __toString() {
        return __CLASS__ . ": [{$this->code}]: {$this->message}
";
    }

    public function customFunction() {
        echo "A custom function for this type of exception
";
    }
}


/**
 * Create a class to test the exception
 */
class TestException
{
    public $var;

    const THROW_NONE    = 0;
    const THROW_CUSTOM  = 1;
    const THROW_DEFAULT = 2;

    function __construct($avalue = self::THROW_NONE) {

        switch ($avalue) {
            case self::THROW_CUSTOM:
                // throw custom exception
                throw new MyException('1 is an invalid parameter', 5);
                break;

            case self::THROW_DEFAULT:
                // throw default one.
                throw new Exception('2 is not allowed as a parameter', 6);
                break;

            default: 
                // No exception, object will be created.
                $this->var = $avalue;
                break;
        }
    }
}


// Example 1
try {
    $o = new TestException(TestException::THROW_CUSTOM);
} catch (MyException $e) {      // Will be caught
    echo "Caught my exception
", $e;
    $e->customFunction();
} catch (Exception $e) {        // Skipped
    echo "Caught Default Exception
", $e;
}

// Continue execution
var_dump($o); // Null
echo "

";


// Example 2
try {
    $o = new TestException(TestException::THROW_DEFAULT);
} catch (MyException $e) {      // Doesn't match this type
    echo "Caught my exception
", $e;
    $e->customFunction();
} catch (Exception $e) {        // Will be caught
    echo "Caught Default Exception
", $e;
}

// Continue execution
var_dump($o); // Null
echo "

";


// Example 3
try {
    $o = new TestException(TestException::THROW_CUSTOM);
} catch (Exception $e) {        // Will be caught
    echo "Default Exception caught
", $e;
}

// Continue execution
var_dump($o); // Null
echo "

";


// Example 4
try {
    $o = new TestException();
} catch (Exception $e) {        // Skipped, no exception
    echo "Default Exception caught
", $e;
}

// Continue execution
var_dump($o); // TestException
echo "

";
?>

Comment

PREVIOUS NEXT
Code Example
Php :: how to link image in laravel 
Php :: laravel blade foreach index value 
Php :: Unable to locate package php7.4 for kali 2021 
Php :: email or phone required in laravel 
Php :: calculate percentage of amount in php 
Php :: php call class method dynamically 
Php :: Load differenet .env file in laravel 
Php :: jQuery is not defined load-scripts.php 
Php :: php ?? 
Php :: accessing json data in php 
Php :: check date PHP 
Php :: wp reserved image size name 
Php :: php sodium extension xampp 
Php :: php-fpm docker 
Php :: calling fucnction in an other function php 
Php :: removing index.php in codeigniter 
Php :: how to read from temp files php 
Php :: how to read sqlite file in php 
Php :: check if checkbox is not checked laravel 8 
Php :: doctrine querybuilder select alias 
Php :: how to convert youtube url to embed code in php 
Php :: How to Connect MySQL Database with PHP Websites 
Php :: laravel password require one letter and one number 
Php :: json stringify to php array 
Php :: laravel constract method 
Php :: php increment and decrement 
Php :: laravel get data in pivot table 
Php :: php require 
Php :: run laravel project on localhost 
Php :: laravel list of models 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =