PHP PDOException Class
Represents an error generated by the PDO extension.
class PDOException extends RuntimeException { /* Propiedades */ public array $errorInfo; protected string $code; /* Propiedades heredadas */ protected string $message = ""; private string $string = ""; protected int $code; protected string $file = ""; protected int $line; private array $trace = []; private ?Throwable $previous = null; /* Métodos heredados */ final public Exception::getMessage(): string final public Exception::getPrevious(): ?Throwable final public Exception::getCode(): int final public Exception::getFile(): string final public Exception::getLine(): int final public Exception::getTrace(): array final public Exception::getTraceAsString(): string public Exception::__toString(): string private Exception::__clone(): void }
Properties
$errorInfo
– It corresponds to PDO::errorInfo() or PDOStatement::errorInfo().
$code
– SQLSTATE error code. You can use the getCode method to access its value.
Inherited properties from RuntimeException
$message – The exception message
$code – The exception code
$file – The filename where the exception originated
$line – The line where the error occurred
$previous – The previously thrown exception
$trace – The stack trace as an array
Methods
The methods that the PDOException class has are inherited from RuntimeException, and they are as follows:
getMessage
Gets the exception message in case we encounter an error or failure while using the PDO extension. This method returns the message associated with the PDO exception that has been thrown.
Description / Descripciónfinal public Exception::getMessage(): stringgetMessage example:
try { $dsn = 'mysql:host=localhost;dbname=nombre_basedatos'; $usuario = 'usuario'; $contrasena = 'contraseña'; $conexion = new PDO($dsn, $usuario, $contrasena); // Aquí podrías realizar operaciones con la base de datos } catch (PDOException $e) { // En caso de error, capturamos la excepción y mostramos el mensaje echo "Error de conexión: " . $e->getMessage(); }
Return
string – The return will be a string, the error we obtained from the previous operation using PDO.
getPrevious
Returns the previous Throwable (interface)
Description / Descripciónfinal public Exception::getPrevious(): ?Throwable
getCode
Gets the code of an exception.
Description / Descripciónfinal public Exception::getCode(): int
getFile
Gets the place where the exception was created.
Description / Descripciónfinal public Exception::getFile(): string
getLine
Gets the line where the exception was created.
Description / Descripciónfinal public Exception::getLine(): int
Concept of Stack Trace in Exceptions:
The stack trace of an exception, also known as a call stack, is a record that shows the sequence of function calls that were executed up to the point where the exception occurred. It is a fundamental tool for debugging errors in software.
getTrace
We will obtain the stack trace as an array
Description / Descripciónfinal public Exception::getTrace(): array
Return
This returned array would be an array of arrays containing information such as function, file, line, arguments, etc.
getTraceAsString
Gets the stack trace as a string
Description / Descripciónfinal public Exception::getTraceAsString(): string