使用try{thow}catch(){}异常处理结构,可以有效的控制多行可能发生异常的代码,基本模型如下:
<?<span>php </span><span>try</span><span> { throw </span><span>new</span> Exception(<span>"</span><span>错误信息</span><span>"</span>;) <span>//</span><span>抛出一个错误</span><span> } </span><span>catch</span>(Exception $e) <span>//</span><span>捕获错误信息,Exception是一个内置的错误处理类</span> <span>{ echo $e</span>->getMessage(); <span>//</span><span>输出错误信息</span> echo $e->getCode(); <span>//</span><span>返回异常代码</span> echo $e->getFile(); <span>//</span><span>返回发生异常的文件</span> echo $e->getLine(); <span>//</span><span>返回发生异常的行号</span> <span>} </span>?>
Exception原形:
<?<span>php </span><span>class</span><span> Exception { </span><span>protected</span> $message = <span>'</span><span>Unknown exception</span><span>'</span>; <span>//</span><span> 异常信息</span> <span>private</span> $<span>string</span>; <span>//</span><span> __toString cache</span> <span>protected</span> $code = <span>0</span>; <span>//</span><span> 用户自定义异常代码</span> <span>protected</span> $file; <span>//</span><span> 发生异常的文件名</span> <span>protected</span> $line; <span>//</span><span> 发生异常的代码行号</span> <span>private</span> $trace; <span>//</span><span> backtrace</span> <span>private</span> $previous; <span>//</span><span> previous exception if nested exception</span> <span>public</span> function __construct($message = <span>null</span>, $code = <span>0</span>, Exception $previous = <span>null</span><span>); final </span><span>private</span> function __clone(); <span>//</span><span> Inhibits cloning of exceptions.</span> <span> final </span><span>public</span> function getMessage(); <span>//</span><span> 返回异常信息</span> final <span>public</span> function getCode(); <span>//</span><span> 返回异常代码</span> final <span>public</span> function getFile(); <span>//</span><span> 返回发生异常的文件名</span> final <span>public</span> function getLine(); <span>//</span><span> 返回发生异常的代码行号</span> final <span>public</span> function getTrace(); <span>//</span><span> backtrace() 数组</span> final <span>public</span> function getPrevious(); <span>//</span><span> 之前的 exception</span> final <span>public</span> function getTraceAsString(); <span>//</span><span> 已格成化成字符串的 getTrace() 信息 </span> <span>public</span> function __toString(); <span>//</span><span> 可输出的字符串</span> <span>} </span>?>
同时,一个try也可以关联多个catch块,可以自定义类继承Exception类来实现
<?<span>php </span><span>class</span> myException1 extends Exception{} <span>//</span><span>根据需要自定义</span> <span>class</span><span> myException2 extends Exception{} </span><span>class</span><span> myException3 extends Exception{} </span><span>try</span><span>{ </span><span>switch</span>(<span>1</span><span>){ </span><span>case</span> <span>1</span>:<span>throw</span> <span>new</span><span> myException1(); </span><span>case</span> <span>2</span>:<span>throw</span> <span>new</span><span> myException2(); </span><span>case</span> <span>3</span>:<span>throw</span> <span>new</span> myException3(); <span>//</span><span>当找不到对应的catch代码块时,将会抛到Exception处理,因为myException3继承了Exception</span> <span> } } </span><span>catch</span><span>(myException1 $e){ echo </span><span>"</span><span>自定义错误1</span><span>"</span><span>; } </span><span>catch</span><span>(myException2 $e){ echo </span><span>"</span><span>自定义错误2</span><span>"</span><span>; } </span><span>catch</span><span>(Exception $e){ echo </span><span>"</span><span>原始异常类</span><span>"</span><span>; } </span>?>