目录

PHP 异常


什么是异常?

异常是描述 PHP 脚本的错误或意外行为的对象。

许多 PHP 函数和类都会引发异常。

用户定义的函数和类也可以抛出异常。

当函数遇到无法使用的数据时,异常是停止函数的好方法。


抛出异常

这个throw语句允许用户定义的函数或方法抛出异常。当抛出异常时,其后面的代码将不会被执行。

如果未捕获异常,则会发生致命错误,并显示 "Uncaught Exception" 消息。

让我们尝试抛出异常而不捕获它:

示例

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

echo divide(5, 0);
?>
亲自试一试 »

结果看起来像这样:

Fatal error: Uncaught Exception: Division by zero in C:\webfolder\test.html:4
Stack trace: #0 C:\webfolder\test.html(9):
divide(5, 0) #1 {main} thrown in C:\webfolder\test.html on line 4

try...catch 语句

为了避免上面例子中的错误,我们可以使用try...catch语句捕获异常并继续该过程。

语法

try {
  code that can throw exceptions
} catch(Exception $e) {
  code that runs when an exception is caught
}

示例

抛出异常时显示消息:

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} catch(Exception $e) {
  echo "Unable to divide.";
}
?>
亲自试一试 »

catch 块指示应捕获什么类型的异常以及可用于访问异常的变量的名称。在上面的例子中,异常的类型是Exception变量名称是$e



try...catch...finally 语句

这个try...catch...finally语句可用于捕获异常。代码在finally无论是否捕获异常,块都将始终运行。如果finally存在时,catch块是可选的。

语法

try {
  code that can throw exceptions
} catch(Exception $e) {
  code that runs when an exception is caught
} finally {
  code that always runs regardless of whether an exception was caught
}

示例

抛出异常时显示一条消息,然后指示进程已结束:

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} catch(Exception $e) {
  echo "Unable to divide. ";
} finally {
  echo "Process complete.";
}
?>
亲自试一试 »

示例

即使未捕获异常也输出字符串:

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} finally {
  echo "Process complete.";
}
?>
亲自试一试 »

异常对象

异常对象包含有关函数遇到的错误或意外行为的信息。

语法

new Exception(message, code, previous)

参数值

Parameter Description
message Optional. A string describing why the exception was thrown
code Optional. An integer that can be used to easily distinguish this exception from others of the same type
previous Optional. If this exception was thrown in a catch block of another exception, it is recommended to pass that exception into this parameter

方法

捕获异常时,下表显示了一些可用于获取异常信息的方法:

Method Description
getMessage() Returns a string describing why the exception was thrown
getPrevious() If this exception was triggered by another one, this method returns the previous exception. If not, then it returns null
getCode() Returns the exception code
getFile() Returns the full path of the file in which the exception was thrown
getLine() Returns the line number of the line of code which threw the exception

示例

输出有关抛出的异常的信息:

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero", 1);
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} catch(Exception $ex) {
  $code = $ex->getCode();
  $message = $ex->getMessage();
  $file = $ex->getFile();
  $line = $ex->getLine();
  echo "Exception thrown in $file on line $line: [Code $code]
  $message";
}
?>
亲自试一试 »

完整的异常参考

如需完整参考,请访问我们的完整的 PHP 异常参考

该参考包含所有异常方法的描述和示例。