Creating a custom Exception from PDOException [closed]

0

Hello.

I need to implement a custom Exception that extends from PDOException and thereby create some methods that return standard messages. So far I have only found unclear examples. Does anyone have a simple example?

    
asked by anonymous 26.10.2015 / 18:01

1 answer

2

A simple example, where the MyPDOException class overrides the PDOException class, so that it can make a custom message when an error occurs using the PDO class.

class MyPDOException extends PDOException { }

class MyClass {
    public function myFunction() {

        try {
            try {

                throw new MyPDOException('Sua mensagem de erro!');
            } catch (MyPDOException $e) {
                throw $e;
            }
        } catch (Exception $e) {
            var_dump($e->getMessage());
        }

    }
}

$foo = new MyClass;
$foo->myFunction();
    
26.10.2015 / 21:46