Vordefinierte Exceptions

Inhaltsverzeichnis

Siehe auch das Kapitel SPL-Exceptions.

Exception

Einführung

Exception ist die Basisklasse für alle Exceptions.

Klassenbeschreibung

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

Eigenschaften

message

Die Exceptionmeldung.

code

Die Fehlernummer der Exception.

file

Der Name der Datei, in der die Exception erzeugt wurde.

line

Die Zeilennummer, in der die Exception erzeugt wurde.

ErrorException

Einführung

Eine Error Exception.

Klassenbeschreibung

ErrorException
class ErrorException extends Exception {
/* Eigenschaften */
protected int $severity ;
/* Geerbte Eigenschaften */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Methoden */
public __construct ([ string $message = "" [, int $code = 0 [, int $severity = 1 [, string $filename = __FILE__ [, int $lineno = __LINE__ [, Exception $previous = NULL ]]]]]] )
final public int getSeverity ( void )
/* Geerbte Methoden */
final public string Exception::getMessage ( void )
final public Exception Exception::getPrevious ( void )
final public mixed Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )
}

Eigenschaften

severity

Die Schwere einer Exception

Beispiele

Beispiel #1 Verwenden von set_error_handler zum Verändern von Fehlermeldungen innerhalb ErrorException.

<?php
function exception_error_handler($severity$message$file$line) {
    if (!(
error_reporting() & $severity)) {
        
// Dieser Fehlercode ist nicht in error_reporting enthalten
        
return;
    }
    throw new 
ErrorException($message0$severity$file$line);
}
set_error_handler("exception_error_handler");

/* Exception ausloesen */
strpos();
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

Fatal error: Uncaught exception 'ErrorException' with message 'strpos() expects at least 2 parameters, 0 given' in /home/bjori/tmp/ex.php:12
Stack trace:
#0 [internal function]: exception_error_handler(2, 'strpos() expect...', '/home/bjori/php...', 12, Array)
#1 /home/bjori/php/cleandocs/test.php(12): strpos()
#2 {main}
  thrown in /home/bjori/tmp/ex.php on line 12