PHP: ErrorException
Giải phóng thời gian, khai phóng năng lực
Phiên bản PHP hỗ trợ: (PHP 5 >= 5.1.0, PHP 7)
Đây là loại lớp thừa kế từ lớp Exception. Nội dung tổng quan của lớp này được thể hiện như dưới đây:
/* Các thuộc tính */
protected int $severity ;
/* Các thuộc tính thừa kế */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Các phương thức */
public __construct ([ string
$message
= "" [, int $code
= 0 [, int $severity
= E_ERROR [,string $filename
= __FILE__ [, int $lineno
= __LINE__ [, Exception $previous
= NULL
]]]]]] )final public int getSeverity ( void )
/* Các phương thức thừa kế */
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 )
}
Thuộc tính
severity
Thuộc tính này thể hiện mức độ nghiêm trọng của ngoại lệ.
Ví dụ
Ví dụ sau sử dụng phương thức set_error_handler() để thay đổi thông báo lỗi của ErrorException.
<?php
function exception_error_handler($severity, $message, $file, $line) {
if (!(error_reporting() & $severity)) {
// Đoạn mã lỗi này không nằm trong thông báo lỗi
return;
}
throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");
/* Kích hoạt ngoại lệ */
strpos();
?>
Output của ví dụ trên có dạng như sau:
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
Giải phóng thời gian, khai phóng năng lực