PHP Try Catch Exceptions如何工作

I'm new to try catch in php, and I was playing around with it.

when I try this one, it works fine

try {

    if (!$connect)
    {
        throw new Exception("it's not working");
    }
} catch (Exception $e) {
    $e->getMessage();
}           

When I try this one, it doesn't work

try {       
    if (!$connect) {
        throw new MyException("it's not working");
    }       
} catch (MyException $e) {
    echo $e->getMessage();
}       

I have only changed the name of the exception, can someone explain where I went wrong pls. thanks

In order to use a custom exception, you need to extend the Exception class:

http://php.net/manual/en/language.exceptions.extending.php

/**
 * Define a custom exception class
 */
class MyException extends Exception
{
    // Redefine the exception so message isn't optional
    public function __construct($message, $code = 0, Exception $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
";
    }
}