Π˜ΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ Exception

(PHP 5, PHP 7, PHP 8)

Π’Π²Π΅Π΄Π΅Π½ΠΈΠ΅

Класс Exception β€” Π±Π°Π·ΠΎΠ²Ρ‹ΠΉ класс для ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΠΈΡ… ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΉ.

ΠžΠ±Π·ΠΎΡ€ класса

class Exception implements Throwable {
/* Бвойства */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private ?Throwable $previous = null;
/* ΠœΠ΅Ρ‚ΠΎΠ΄Ρ‹ */
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
final public function getMessage(): string
final public function getPrevious(): ?Throwable
final public function getCode(): int
final public function getFile(): string
final public function getLine(): int
final public function getTrace(): array
final public function getTraceAsString(): string
public function __toString(): string
private function __clone(): void
}

Бвойства

message

ВСкст ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ

code

Код ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ

file

НазваниС Ρ„Π°ΠΉΠ»Π°, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ Π²ΠΎΠ·Π½ΠΈΠΊΠ»ΠΎ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅

line

НомСр строки, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Π²ΠΎΠ·Π½ΠΈΠΊΠ»ΠΎ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅

previous

Π Π°Π½Π΅Π΅ Π²Ρ‹Π±Ρ€ΠΎΡˆΠ΅Π½Π½ΠΎΠ΅ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅

string

Π‘Ρ‚Ρ€ΠΎΠΊΠΎΠ²ΠΎΠ΅ прСдставлСниС трассировки стСка

trace

Врассировка стСка Π² Π²ΠΈΠ΄Π΅ массива

Π‘ΠΎΠ΄Π΅Ρ€ΠΆΠ°Π½ΠΈΠ΅

  • Exception::__construct β€” Π‘ΠΎΠ·Π΄Π°Ρ‚ΡŒ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅
  • Exception::getMessage β€” ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ сообщСниС ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ
  • Exception::getPrevious β€” Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ ΠΏΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰ΠΈΠΉ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚, Ρ€Π΅Π°Π»ΠΈΠ·ΡƒΡŽΡ‰ΠΈΠΉ Throwable
  • Exception::getCode β€” ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ ΠΊΠΎΠ΄ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ
  • Exception::getFile β€” ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ Ρ„Π°ΠΉΠ», Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ Π²ΠΎΠ·Π½ΠΈΠΊΠ»ΠΎ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅
  • Exception::getLine β€” ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ строку, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Π²ΠΎΠ·Π½ΠΈΠΊΠ»ΠΎ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅
  • Exception::getTrace β€” ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ трассировку стСка
  • Exception::getTraceAsString β€” ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ трассировку стСка Π² Π²ΠΈΠ΄Π΅ строки
  • Exception::__toString β€” Π‘Ρ‚Ρ€ΠΎΠΊΠΎΠ²ΠΎΠ΅ прСдставлСниС ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ
  • Exception::__clone β€” ΠšΠ»ΠΎΠ½ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 3 notes

up
102
whysteepy at gmail dot com ΒΆ
8 years 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
32
cHao ΒΆ
11 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
9
shaman_master at list dot ru ΒΆ
6 years ago
Note: this documentation not full, ReflectionObject::export($exception):
<?php
Object of class [  class Exception implements Throwable ] {
  - Properties [7] {
    Property [  protected $message ]
    Property [  private $string ]
    Property [  protected $code ]
    Property [  protected $file ]
    Property [  protected $line ]
    Property [  private $trace ]
    Property [  private $previous ]
  }
  - Methods [11] {
    Method [  final private method __clone ] {
    }

    Method [  public method __construct ] {

      - Parameters [3] {
        Parameter #0 [  $message ]
        Parameter #1 [  $code ]
        Parameter #2 [  $previous ]
      }
    }

    Method [  public method __wakeup ] {
    }

    Method [  final public method getMessage ] {
    }

    Method [  final public method getCode ] {
    }

    Method [  final public method getFile ] {
    }

    Method [  final public method getLine ] {
    }

    Method [  final public method getTrace ] {
    }

    Method [  final public method getPrevious ] {
    }

    Method [  final public method getTraceAsString ] {
    }

    Method [  public method __toString ] {
    }
  }
}
?>

Missed:

    Property [  private $string ]
    Property [  private $trace ]
    Property [  private $previous ]

    Method [  public method __wakeup ] {
    }