Ném ngoại lệ vào PHP
Ném một ngoại lệ PHP chung gần như đơn giản như âm thanh. Tất cả chỉ cần khởi tạo một đối tượng ngoại lệ, với tham số đầu tiên của hàm tạo ngoại lệ là thông báo lỗi và sau đó, "ném" nó.
throw new Exception['Exception message']
Điều quan trọng nhất cần lưu ý là thông điệp. Được xác định trong hàm tạo và được truy cập thông qua phương thức getMessage [], thông báo là lỗi có thể đọc được của con người thường có thể liên quan đến người dùng cuối.
Lớp ngoại lệ
Để sử dụng hiệu quả hơn các ngoại lệ trong ứng dụng của bạn, điều quan trọng là phải hiểu cách tạo và ném ngoại lệ PHP của riêng bạn. Nhưng trước khi chúng ta ném các ngoại lệ PHP tùy chỉnh, trước tiên chúng ta hãy xem một ngoại lệ dưới mui xe và cách xác định của riêng bạn, bắt đầu với lớp ngoại lệ toàn cầu mà tất cả các ngoại lệ PHP đều xuất phát từ:
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
Như chúng ta có thể thấy từ định nghĩa ở trên, mọi ngoại lệ tích hợp bao gồm một vài thuộc tính có thể định cấu hình: một thông báo ngoại lệ, mã ngoại lệ, tên tệp nguồn của ngoại lệ và số dòng của ngoại lệ trong tệp nguồn. Thông tin này là những gì được sử dụng để tạo ra một đối tượng Exception có thể đọc được của con người và có thể chẩn đoán được.
Ném các ngoại lệ PHP tùy chỉnh
Về cốt lõi của họ, mọi ngoại lệ là một phần mở rộng của lớp ngoại lệ toàn cầu. Điều này có nghĩa là việc tạo ra một ngoại lệ tùy chỉnh, ở dạng cơ bản nhất, chỉ yêu cầu một lớp mở rộng lớp ngoại lệ tích hợp.
namespace Custom;
class Exception extends Exception { }
Với ngoại lệ tùy chỉnh này được tạo, sau đó chúng ta có thể ném nó như bất kỳ ngoại lệ nào khác:
throw new CustomException['Exception message'];
Ưu điểm để kế thừa từ lớp ngoại lệ tích hợp là chúng ta có thể mở rộng chức năng cốt lõi của các ngoại lệ mặc định. Bằng cách ghi đè các thuộc tính của lớp như mã, tệp, dòng và thông báo hoặc phương thức __toString[]
, chúng ta có thể ép dữ liệu ngoại lệ vào một định dạng chúng ta có thể làm việc.
namespace Custom;
class Exception extends Exception {
protected $details;
public function __construct[$details] {
$this->details = $details;
parent::__construct[];
}
public function __toString[] {
return 'I am an exception. Here are the deets: ' . $this->details;
}
}
Ghi vào nhật ký lỗi
Ngoài việc ném ngoại lệ, PHP cũng hỗ trợ ghi trực tiếp vào nhật ký lỗi [thêm về đó là lỗi của PHP đã được ghi?]. Chức năng Error_log có tên APTLY có thể được sử dụng để ghi các tin nhắn thô vào nhật ký lỗi mà không cần thực thi chương trình làm gián đoạn. Điều này rất hữu ích khi bạn cần theo dõi dữ liệu gỡ lỗi hoặc theo dõi các ngoại lệ PHP bị bắt và xử lý.
bool error_log [ string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] ]
Tùy thuộc vào $message_type
,
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
0 thường chỉ yêu cầu một tham số: một thông báo. Mặc dù các tham số khác có thể được sử dụng để chỉ đạo thông báo lỗi nên sử dụng, trường hợp sử dụng phổ biến nhất là sử dụng các tùy chọn mặc định để ghi trực tiếp vào bộ ghi hệ thống của PHP.Tom Polomsk ¶
- Sawsan ¶
10 năm trước
Daan ¶
1 năm trướcException class or a subclass of Exception. Trying to throw an object that is not will result in a PHP Fatal Error.
Mục đích của lớp ngoại lệ chung trong PHP là gì?
Các ngoại lệ được sử dụng để thay đổi luồng thông thường của tập lệnh nếu xảy ra lỗi được chỉ định.
Ngoại lệ PHP là gì?
Một ngoại lệ là một đối tượng mô tả một lỗi hoặc hành vi bất ngờ của tập lệnh PHP. Các trường hợp ngoại lệ được ném bởi nhiều chức năng và lớp PHP. Các chức năng và lớp học do người dùng xác định cũng có thể ném ngoại lệ. Ngoại lệ là một cách tốt để ngăn chặn một chức năng khi nó xuất hiện dữ liệu mà nó không thể sử dụng.
Các loại ngoại lệ trong PHP là gì?set_exception_handler[].
Kể từ Php 7.1.0, một khối
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
2 có thể chỉ định nhiều ngoại lệ bằng cách sử dụng ký tự đường ống [throw new CustomException['Exception message'];
5]. Điều này rất hữu ích cho khi các ngoại lệ khác nhau từ các phân cấp lớp khác nhau được xử lý giống nhau.Kể từ Php 8.0.0, tên biến cho một ngoại lệ bị bắt là tùy chọn. Nếu không được chỉ định, khối
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
2 vẫn sẽ thực thi nhưng sẽ không có quyền truy cập vào đối tượng ném.class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
6
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
Một khối
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
6 cũng có thể được chỉ định sau hoặc thay vì các khối class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
2. Mã trong khối class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
6 sẽ luôn được thực thi sau các khối class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
3 và class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
2, bất kể ngoại lệ có bị ném hay không và trước khi thực hiện bình thường tiếp tục.Một tương tác đáng chú ý là giữa khối
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
6 và câu lệnh namespace Custom;
class Exception extends Exception {
protected $details;
public function __construct[$details] {
$this->details = $details;
parent::__construct[];
}
public function __toString[] {
return 'I am an exception. Here are the deets: ' . $this->details;
}
}
4. Nếu một câu lệnh namespace Custom;
class Exception extends Exception {
protected $details;
public function __construct[$details] {
$this->details = $details;
parent::__construct[];
}
public function __toString[] {
return 'I am an exception. Here are the deets: ' . $this->details;
}
}
4 gặp phải bên trong các khối class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
3 hoặc class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
2, khối class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
6 vẫn sẽ được thực thi. Hơn nữa, câu lệnh namespace Custom;
class Exception extends Exception {
protected $details;
public function __construct[$details] {
$this->details = $details;
parent::__construct[];
}
public function __toString[] {
return 'I am an exception. Here are the deets: ' . $this->details;
}
}
4 được đánh giá khi gặp phải, nhưng kết quả sẽ được trả về sau khi khối class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
6 được thực thi. Ngoài ra, nếu khối class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
6 cũng chứa câu lệnh namespace Custom;
class Exception extends Exception {
protected $details;
public function __construct[$details] {
$this->details = $details;
parent::__construct[];
}
public function __toString[] {
return 'I am an exception. Here are the deets: ' . $this->details;
}
}
4, giá trị từ khối class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
6 được trả về.bool error_log [ string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] ]
4
bool error_log [ string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] ]
Nếu một ngoại lệ được phép bong bóng lên phạm vi toàn cầu, nó có thể bị bắt bởi một trình xử lý ngoại lệ toàn cầu nếu được đặt. Hàm set_exception_handler [] có thể đặt hàm sẽ được gọi thay cho khối
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
2 nếu không có khối nào được gọi. Hiệu ứng về cơ bản giống như khi toàn bộ chương trình được bọc trong khối ____ 13 -________ 12 với chức năng đó là class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
2.set_exception_handler[] function can set a function that will be called in place of a class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
2 block if no other block is invoked. The effect is essentially the same as if the entire program were wrapped in a
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
3-class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
2 block with that function as the class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
2. Ghi chú
Ghi chú::
Các hàm PHP nội bộ chủ yếu sử dụng báo cáo lỗi, chỉ các tiện ích mở rộng hướng đối tượng hiện đại sử dụng các ngoại lệ. Tuy nhiên, các lỗi có thể dễ dàng được dịch sang các ngoại lệ với errorException. Kỹ thuật này chỉ hoạt động với các lỗi không gây tử vong, tuy nhiên.
Ví dụ #3 Chuyển đổi báo cáo lỗi thành ngoại lệ
9bool error_log [ string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] ]
Ví dụ
Ví dụ #4 ném một ngoại lệ
0.2 Caught exception: Division by zero. Hello World0
0.2 Caught exception: Division by zero. Hello World1
0.2 Caught exception: Division by zero. Hello World2
Ví dụ trên sẽ xuất ra:
0.2 Caught exception: Division by zero. Hello World
Ví dụ #5 Xử lý ngoại lệ với khối
6class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
0.2 Caught exception: Division by zero. Hello World0
0.2 Caught exception: Division by zero. Hello World1
0.2 Caught exception: Division by zero. Hello World6
Ví dụ trên sẽ xuất ra:
0.2 First finally. Caught exception: Division by zero. Second finally. Hello World
Ví dụ #5 Xử lý ngoại lệ với khối
6class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
0.2 Caught exception: Division by zero. Hello World9
0.2 First finally. Caught exception: Division by zero. Second finally. Hello World0
0.2 First finally. Caught exception: Division by zero. Second finally. Hello World1
Ví dụ trên sẽ xuất ra:
Ví dụ #5 Xử lý ngoại lệ với khối
6class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
0.2 First finally. Caught exception: Division by zero. Second finally. Hello World2
0.2 First finally. Caught exception: Division by zero. Second finally. Hello World3
0.2 First finally. Caught exception: Division by zero. Second finally. Hello World4
Ví dụ trên sẽ xuất ra:
Ví dụ #5 Xử lý ngoại lệ với khối
6class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
0.2 First finally. Caught exception: Division by zero. Second finally. Hello World2
0.2 First finally. Caught exception: Division by zero. Second finally. Hello World3
0.2 First finally. Caught exception: Division by zero. Second finally. Hello World7
Ví dụ trên sẽ xuất ra:
Ví dụ #5 Xử lý ngoại lệ với khối
6class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
Ví dụ #6 Tương tác giữa khối
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
6 và namespace Custom;
class Exception extends Exception {
protected $details;
public function __construct[$details] {
$this->details = $details;
parent::__construct[];
}
public function __toString[] {
return 'I am an exception. Here are the deets: ' . $this->details;
}
}
40.2 First finally. Caught exception: Division by zero. Second finally. Hello World8
0.2 First finally. Caught exception: Division by zero. Second finally. Hello World9
__toString[]
0Ví dụ #7 Ngoại lệ lồng nhau
Ví dụ #6 Tương tác giữa khối
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
6 và namespace Custom;
class Exception extends Exception {
protected $details;
public function __construct[$details] {
$this->details = $details;
parent::__construct[];
}
public function __toString[] {
return 'I am an exception. Here are the deets: ' . $this->details;
}
}
40.2 First finally. Caught exception: Division by zero. Second finally. Hello World8
0.2 First finally. Caught exception: Division by zero. Second finally. Hello World9
__toString[]
3Ví dụ #7 Ngoại lệ lồng nhau ¶
Ví dụ #8 Xử lý ngoại lệ Multi Catch
__toString[]
4
__toString[]
5
__toString[]
6
__toString[]
7Ví dụ #9 bỏ qua biến bị bắt ¶
Chỉ được phép trong Php 8.0.0 trở lên.
__toString[]
8
__toString[]
9
Hỏi tại Nilpo Dot Com ¶ ¶
13 năm trước
$message_type
1
$message_type
2
$message_type
3
$message_type
4Johan ¶ ¶
11 năm trước
$message_type
5
$message_type
6
$message_type
7
$message_type
0 $message_type
0 ¶
Christof+php [at] insypro.com ¶
$message_type
9
5 năm trước ¶
Shot [Piotr Szotkowski] ¶
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
00class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
01class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
02$message_type
014 năm trước ¶
Simo ¶
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
04class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
05class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
067 năm trước ¶
13 năm trước
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
07class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
08$message_type
0Johan ¶ ¶
11 năm trước
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
10class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
11$message_type
0 $message_type
0 ¶
Christof+php [at] insypro.com ¶
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
135 năm trước ¶
Shot [Piotr Szotkowski] ¶
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
14class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
15class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
16class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
1714 năm trước ¶
Simo ¶
class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
18class Exception {
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct[$message = null, $code = 0, Exception $previous = null];
final private function __clone[]; // Inhibits cloning of exceptions.
final public function getMessage[]; // message of exception
final public function getCode[]; // code of exception
final public function getFile[]; // source filename
final public function getLine[]; // source line
final public function getTrace[]; // an array of the backtrace[]
final public function getPrevious[]; // previous exception
final public function getTraceAsString[]; // formatted string of trace
// Overrideable
public function __toString[]; // formatted string for display
}
19$message_type
0