在使用 laravel 框架(或者其他框架)的时候,我们都知道在抛出异常报错的时候,其实这个异常并不像我们平时使用
throw new Exception('程序异常');
抛出的页面那么难看,另外,在抛出异常后,框架会做异常日志记录。
- 切记,在执行完毕
set_exception_handler
后,记得使用 restore_exception_handler 函数将异常处理还原
<?php
// 注册全局异常处理程序
set_exception_handler(function (Exception $e) {
// 处理并记录异常
});
// 注册其他的异常处理程序...
// 还原成之前的全局异常处理程序
restore_exception_handler();
demo-注意行号
打印结果
- 注意
所有的报错类都是集成 Exception 类的。所以,你如果需要打印错误信息,行号等信息,是可以直接调用 public 方法的。我在 demo 中是打印的报错对象
在实际应用中,我们可以根据是否已经打开 debug 等配置,来决定怎么处理报错,并且可以在抛出报错之前可以将报错存入日志
class Exception implements Throwable {
protected $message;
protected $code;
protected $file;
protected $line;
/**
* Clone the exception
* Tries to clone the Exception, which results in Fatal error.
* @link https://php.net/manual/en/exception.clone.php
* @return void
* @since 5.1.0
*/
final private function __clone() { }
/**
* Construct the exception. Note: The message is NOT binary safe.
* @link https://php.net/manual/en/exception.construct.php
* @param string $message [optional] The Exception message to throw.
* @param int $code [optional] The Exception code.
* @param Throwable $previous [optional] The previous throwable used for the exception chaining.
* @since 5.1.0
*/
public function __construct($message = "", $code = 0, Throwable $previous = null) { }
/**
* Gets the Exception message
* @link https://php.net/manual/en/exception.getmessage.php
* @return string the Exception message as a string.
* @since 5.1.0
*/
final public function getMessage() { }
/**
* Gets the Exception code
* @link https://php.net/manual/en/exception.getcode.php
* @return mixed|int the exception code as integer in
* <b>Exception</b> but possibly as other type in
* <b>Exception</b> descendants (for example as
* string in <b>PDOException</b>).
* @since 5.1.0
*/
final public function getCode() { }
/**
* Gets the file in which the exception occurred
* @link https://php.net/manual/en/exception.getfile.php
* @return string the filename in which the exception was created.
* @since 5.1.0
*/
final public function getFile() { }
/**
* Gets the line in which the exception occurred
* @link https://php.net/manual/en/exception.getline.php
* @return int the line number where the exception was created.
* @since 5.1.0
*/
final public function getLine() { }
/**
* Gets the stack trace
* @link https://php.net/manual/en/exception.gettrace.php
* @return array the Exception stack trace as an array.
* @since 5.1.0
*/
final public function getTrace() { }
/**
* Returns previous Exception
* @link https://php.net/manual/en/exception.getprevious.php
* @return Exception the previous <b>Exception</b> if available
* or null otherwise.
* @since 5.3.0
*/
final public function getPrevious() { }
/**
* Gets the stack trace as a string
* @link https://php.net/manual/en/exception.gettraceasstring.php
* @return string the Exception stack trace as a string.
* @since 5.1.0
*/
final public function getTraceAsString() { }
/**
* String representation of the exception
* @link https://php.net/manual/en/exception.tostring.php
* @return string the string representation of the exception.
* @since 5.1.0
*/
public function __toString() { }
public function __wakeup() { }
}
报错处理
<?php
set_error_handler(//error handler function
function ($errno, $errstr, $errfile, $errline) {
echo "错误编号: $errno<br>";
echo "错误信息: $errstr<br>";
echo "出错文件: $errfile<br>";
echo "出错行号: $errline<br>";
die;
});
echo $arr[1];
image.png
image.png
网友评论