Creating a Custom Exception from Pdoexception

Asked

Viewed 128 times

0

Hello.

I need to implement a custom Exception that extends from Pdoexception and with it create some methods that return standard messages. So far I have only found unclear examples. Someone has some simple example?

  • Good afternoon Raphael, sorry, not clear, you want to understand how the implementation works in a Exception existing, or you want to understand how the Exceptions? Edit the question to make your need more evident. I’m sure it will take the comment as a constructive criticism.

1 answer

2


A simple example, where the Mypdoexception class endeavors to the Pdoexception class, which can then 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.