当程序的正常流程由于某些事件而被中断或干扰时,该事件称为异常。用于处理异常的方法称为异常处理程序,而将这种现象称为异常处理。
如果有可能发生某些特定错误,则可以通过异常进行管理。指定的错误和特定的错误也称为异常,并通过对其进行管理来学习对异常的处理以修改代码。
发生异常时会发生什么?
通常,当发生异常时,会发生以下情况:
该代码按其当前状态保存。
将有一个预定义的(默认)函数来处理将被调用的PHP中的异常。
现在,它取决于此默认异常处理程序函数决定采取的操作。它可以从其保留的地方或其他行继续执行代码,或者完全终止代码执行。
异常应如何处理?
有一些方法可以在运行代码时轻松处理并防止障碍的发生:
尝试,捕捉并扔
定制的异常处理功能
一次处理许多异常
再次引发异常
机顶盒异常处理功能
尝试,捕捉和投掷方法
使用此方法很容易。需要遵循一些步骤。他们是-
在“ try”块中编写可以生成异常的代码。在没有异常的情况下,代码将以正常方式工作,并且“ try”块的执行不会中断或影响。
如果代码可能生成某些异常,则可以由生成该异常,并且每个“抛出”都需要一个“ catch”。
一个“抓”块捕获这是在代码中引发和做什么,当它被捉住程序员的编码应做例外。
程序:
<?php //创建一个带有异常的函数 function enter($roll_no) { try { //检查是否 if($roll_no < 0) { throw new Exception("Roll Number you have entered ".$roll_no. " is not positive \n"); } } catch(Exception $e) { echo 'Message: ' .$e->getMessage(); exit(0); } echo("Your entered roll number is ".$roll_no . "\n"); } enter(9); enter(-4); ?>
输出:
Your entered roll number is 9 Message: Roll Number you have entered -4 is not positive
定制的异常处理功能
为了以更好的方式处理异常,需要创建一个包含异常处理功能的类。当代码中发生异常时,将调用此函数。此类只是异常类的自定义扩展。
创建类的程序:
<?php class ex extends Exception { function Message() { //错误消息 $Msg = 'The code had an attempt to divide a number by zero error on line no '.$this->getLine(). "\n" . 'Hence the number is now divided by 1 rather than 0'. "\n" ; return $Msg; } } function divide($a,$b) { try { //检查是否 divisor is 0 if($b == 0) { throw new ex($b); } } catch (ex $e) { //显示自定义消息 echo $e->Message(); //对错误采取适当的措施...- //仅打印一条消息 $b=1; } //通过纠正错误(如果被捕获)来返回有效值 return $a/$b; } //这不会产生任何异常 echo ("When the 15 is divided by 3 the result is "); echo(divide(15,3)."\n"); echo ("When the 4 is divided by 0 the result is \n"); //会导致异常 echo(divide(4,0)); ?>
输出:
When the 15 is divided by 3 the result is 5 When the 4 is divided by 0 the result is The code had an attempt to divide a number by zero error on line no 17 Hence the number is now divided by 1 rather than 0 4
一次处理许多异常
一次捕获多次并抛出异常可以处理许多异常。每个投掷都有一个对应的接球区。
程序:
<?php class ex extends Exception { function Message() { //错误消息 $Msg = 'The code had an attempt to divide a number by zero error on line no '.$this->getLine(). "\n" . 'Hence the number is now divided by 1 rather than 0'. "\n" ; return $Msg; } } class ex1 extends Exception { function Message1() { //错误消息 $Msg1 = 'The code had an attempt to divide a number by negative number on line no '.$this->getLine(). "\n" . 'Hence the number is now divided by modulus of the divisor'. "\n" ; return $Msg1; } } function jsm($a,$b) { try { //检查是否 if($b == 0) { throw new ex($b); } if($b < 0) { throw new ex1($b); } } catch (ex $e) { //显示自定义消息 echo $e->Message(); $b=1; } catch (ex1 $e) { //显示自定义消息 echo $e->Message1(); //采取行动 $b= -$b; } return $a/$b; } //这不会产生任何异常 echo ("When the 15 is divided by -3 the result is \n"); echo(jsm(15,-3)."\n"); echo ("When the 4 is divided by 0 the result is \n"); //会导致异常 echo(jsm(4,0)); ?>
输出:
The code had an attempt to divide a number by negative number on line no 31 Hence the number is now divided by modulus of the divisor 5 When the 4 is divided by 0 the result is The code had an attempt to divide a number by zero error on line no 27 Hence the number is now divided by 1 rather than 0 4
再次引发异常
可能希望以与默认方法完全不同的方式处理异常。可以在“ catch”块内再次引发异常。由系统生成的隐藏错误使程序更易于用户使用,因为许多错误对于用户无用地显示在屏幕上。
程序:
<?php class ex extends Exception { function Message() { //错误消息 $Msg = 'The code had an attempt to divide a number by zero error on line no '.$this->getLine(). "\n" . 'Hence the number is now divided by 1 rather than 0'. "\n" ; return $Msg; } } $dividend=20; $divisor=0; try { try { if($divisor ==0) { throw new Exception($divisor); } } catch(Exception $e) { echo("\nChanging 0 divisor to 1 \n"); $divisor =1; throw new ex($divisor); } } catch (ex $e) { echo $e->Message(); } echo("\n Quotient ".($dividend/$divisor)); ?>
输出:
Changing 0 divisor to 1 The code had an attempt to divide a number by zero error on line no 21 Hence the number is now divided by 1 rather than 0 Quotient 20
Set_exception_handler()函数:
该功能使程序员可以修改功能以制作错误处理功能。
程序:
<?php function ex($exception) { echo "<b>Exception:</b> " . $exception->getMessage(); } set_exception_handler('ex'); $no=0; $factorial=1; //由于no = 0,所以如果执行块 if($no==0) //引发新异常后的任何代码行都不会 //工作和代码结束 throw new Exception('Uncaught Exception occurred'); for($i=$no;$i>0;$i--) $factorial=$factorial * $i; echo("answer ".$factorial); ?>
输出:
<b>Exception:</b> Uncaught Exception occurred
程序:
<?php function ex($exception) { echo "<b>Exception:</b> " . $exception->getMessage(); } set_exception_handler('ex'); $no=5; $factorial=1; //如果为假且未执行 if($no==0) throw new Exception('Uncaught Exception occurred'); for($i=$no;$i>0;$i--) $factorial=$factorial * $i; echo("answer ".$factorial); ?>
输出:
answer 120
重要注意事项:
尝试捕获异常时应记住一些规则-
每个“ try”或“ throw”块都必须有一个对应的“ catch”块。
可以通过使用不同的“ catch”块来捕获各种类型的异常。
如果代码部分更有可能产生错误和异常,则最好将其包含在“ try”块中。