Rumah > php教程 > php手册 > php异常处理,php异常捕获

php异常处理,php异常捕获

WBOY
Lepaskan: 2016-06-13 08:39:54
asal
1500 orang telah melayarinya

php异常处理,php异常捕获

使用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>?>
Salin selepas log masuk

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>?> 
Salin selepas log masuk

同时,一个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>?>
Salin selepas log masuk

 

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan