文字

Exception

(PHP 5 >= 5.1.0)

简介

Exception 是所有异常的基类。

类摘要

Exception {
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
public __construct ([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]] )
final public string getMessage ( void )
final public Exception getPrevious ( void )
final public int 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 )
}

属性

message

异常消息内容

code

异常代码

file

抛出异常的文件名

line

抛出异常在该文件中的行号

Table of Contents

  • Exception::__construct — 异常构造函数
  • Exception::getMessage — 获取异常消息内容
  • Exception::getPrevious — 返回异常链中的前一个异常
  • Exception::getCode — 获取异常代码
  • Exception::getFile — 获取发生异常的程序文件名称
  • Exception::getLine — 获取发生异常的代码在文件中的行号
  • Exception::getTrace — 获取异常追踪信息
  • Exception::getTraceAsString — 获取字符串类型的异常追踪信息
  • Exception::__toString — 将异常对象转换为字符串
  • Exception::__clone — 异常克隆

用户评论:

[#1] cHao [2014-09-27 18:34:51]

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.

[#2] altieresdelsent at gmail dot com [2012-09-28 19:40:50]

when you are using xdebug, exceptions message will never be shown if you use any encoding different than UTF-8, so if you are using any database with translated messages like oracle, you should ALWAYS, always, throw a exception like this 

throw new Exception(utf8_encode($message),$code), character like ?,??,?, will make the exception message fail to be shown, if you are not using xdebug ( I do think you should at least try), this code will not affect your page.

上一篇: 下一篇: