Mid-Atlantic Developer Conference

Exception

(PHP 5, PHP 7)

Introducción

Exception es la clase base para todas las excepciones en PHP 5, y la clase base para todas las excepciones de usuario en PHP 7.

En PHP 7, Exception implementa la interfaz Throwable.

Sinopsis de la Clase

Exception {
/* Propiedades */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Métodos */
public __construct ([ string $message = "" [, int $code = 0 [, Throwable $previous = NULL ]]] )
final public getMessage ( void ) : string
final public getPrevious ( void ) : Exception
final public getCode ( void ) : mixed
final public getFile ( void ) : string
final public getLine ( void ) : int
final public getTrace ( void ) : array
final public getTraceAsString ( void ) : string
public __toString ( void ) : string
final private __clone ( void ) : void
}

Propiedades

message

El mensaje de la excepción

code

El código de la excepción

file

El nombre del fichero donde se originó la excepción

line

La línea donde se originó la excepción

Tabla de contenidos

add a note add a note

User Contributed Notes 4 notes

up
25
whysteepy at gmail dot com
1 year ago
Lists of Throwable and Exception tree as of 7.2.0

    Error
      ArithmeticError
        DivisionByZeroError
      AssertionError
      ParseError
      TypeError
        ArgumentCountError
    Exception
      ClosedGeneratorException
      DOMException
      ErrorException
      IntlException
      LogicException
        BadFunctionCallException
          BadMethodCallException
        DomainException
        InvalidArgumentException
        LengthException
        OutOfRangeException
      PharException
      ReflectionException
      RuntimeException
        OutOfBoundsException
        OverflowException
        PDOException
        RangeException
        UnderflowException
        UnexpectedValueException
      SodiumException

Find the script and output in the following links:
https://gist.github.com/mlocati/249f07b074a0de339d4d1ca980848e6a
https://3v4l.org/sDMsv

posted by someone here http://php.net/manual/en/class.throwable.php
up
34
cHao
4 years ago
Note that an exception's properties are populated when the exception is *created*, not when it is thrown.  Throwing the exception does not seem to modify them.

Among other things, this means:

* The exception will blame the line that created it, not the line that threw it.

* Unlike in some other languages, rethrowing an exception doesn't muck up the trace.

* A thrown exception and an unthrown one look basically identical.  On my machine, the only visible difference is that a thrown exception has an `xdebug_message` property while an unthrown one doesn't.  Of course, if you don't have xdebug installed, you won't even get that.
up
1
maxalmonte14 at hotmail dot com
1 year ago
The reason because Exception class does not implement the Throwable interface is Exception exists since PHP5, while Throwable was defined in PHP7! So, Throwable interface is only for core use purpose, you cannot implement it in your own classes, if you want to make your classes "throwable" you have to extend Exception class.
up
-54
fazlidddin dot uzbek at yahoo dot com
1 year ago
Must be
Exception implements Throwable
{
...
}

instead of

Exception
{

}
To Top