Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrong parameters for exception

Tags:

exception

php

I am getting the following error message:

Wrong parameters for Exception([string $exception [, long $code [, Exception $previous = NULL]]])

Here is my code:

class DAOException extends Exception {

function __construct($message, $code = 0, Exception $previous = null){
    parent::__construct($message, $code, $previous);
}

I try to make my own exception but it keep saying that i have an error at this line:

parent::__construct($message, $code, $previous).

Here is an example of when I could call this exception:

public function add(FilmDTO $filmDTO){
        try{
            $addPreparedStatement = parent::getConnection()->prepare(FilmDAO::ADD_REQUEST);
            $addPreparedStatement->bindParam(':titre', $filmDTO->getTitre());
            $addPreparedStatement->bindParam(':duree', $filmDTO->getDuree());
            $addPreparedStatement->bindParam(':realisateur', $filmDTO->getRealisateur());
            $addPreparedStatement->execute();
        } catch(PDOException $pdoException){
            throw new DAOException($pdoException->getMessage(), $pdoException->getCode(), $pdoException);
        }

    }
like image 446
LVB Avatar asked Mar 10 '26 09:03

LVB


1 Answers

This is because the PDO exception can have a code that is alphanumeric, whereas the exception can only have an integer code. So at the point in the DAO constructor where you pass the code you've been given (the PDO code - a string) to the exception construct, it's not having it.

You can get around this by casting the code to an integer in your DAOException constructor. If you need the full string code (I'm not sure if it gives you any more helpful information) you can always append or prepend it to the message string (again, in the DAOException constructor)

like image 158
hessodreamy Avatar answered Mar 13 '26 03:03

hessodreamy