21xrx.com
登录
文章检索 我的文章 写文章
PHP 异常捕获
2021-07-22 18:24:30 深夜i     --     --
P H P


什么异常捕获?

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

 

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

异常是停止函数的好方法,当它遇到它不能的数据时用。


抛一个异常

throw语句允许用户定义并抛出异常的功能或方法。 当抛出异常时,代码 遵循它不会被执行。

如果没有捕获异常,则“未捕获的致命错误将发生致命的错误 “消息。

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

<?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.php:4
Stack trace: #0 C:\webfolder\test.php(9):
divide(5, 0) #1 {main} thrown in C:\webfolder\test.php 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 Block表示应该捕获哪种类型的异常和名称 可用于访问异常的变量。 在上面的例子中,类型的类型异常是例外,变量名称为$e。


try...catch......finally声明了

try...catch......finally后声明可用于捕捉异常。 代码在 最后,无论是否捕获异常,它都会始终运行。 如果 最后存在,捕获块是可选的。

句法

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 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异常参考。

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


 

 

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复