异常是程序本身可以处理的意外程序结果。PHP中的异常处理与所有编程语言中的异常处理几乎类似。
PHP为此提供了以下专门的关键字。
异常处理相对于错误处理有以下主要优点:
错误处理代码与普通代码分离:在传统的错误处理代码中,总是有一个if-else块来处理错误。这些条件和处理错误的代码混在一起,导致代码难以阅读。使用try-catch块,代码变得易于阅读。
错误类型的分组:在PHP中,基本类型和对象都可以作为异常抛出。它可以创建异常对象的层次结构,在命名空间或类中分组异常,并根据类型对它们进行分类。
示例:
<?php
// PHP Program to illustrate normal
// try catch block code
function demo($var) {
echo " Before try block";
try {
echo "\n Inside try block";
// If var is zero then only if will be executed
if($var == 0)
{
// If var is zero then only exception is thrown
throw new Exception('Number is zero.');
// This line will never be executed
echo "\n After throw (It will never be executed)";
}
}
// Catch block will be executed only
// When Exception has been thrown by try block
catch(Exception $e) {
echo "\n Exception Caught", $e->getMessage();
}
// This line will be executed whether
// Exception has been thrown or not
echo "\n After catch (will be always executed)";
}
// Exception will not be raised
demo(5);
// Exception will be raised here
demo(0);
?>
output
Before try block
Inside try block
After catch (will be always executed)
Before try block
Inside try block
Exception CaughtNumber is zero.
After catch (will be always executed)
示例2
<?php
// PHP Program to illustrate normal
// try catch block code
function demo($var) {
echo " Before try block";
try {
echo "\n Inside try block";
// If var is zerothen only if will be executed
if($var == 0) {
// If var is zero then only exception is thrown
throw new Exception('Number is zero.');
// This line will never be executed
echo "\n After throw it will never be executed";
}
}
// Catch block will be executed only
// When Exception has been thrown by try block
catch(Exception $e) {
echo "\n Exception Caught" . $e->getMessage();
}
finally {
echo "\n Here cleanup activity will be done";
}
// This line will be executed whether
// Exception has been thrown or not
echo "\n After catch it will be always executed";
}
// Exception will not be raised
demo(5);
// Exception will be raised here
demo(0);
?>
output
Before try block
Inside try block
Here cleanup activity will be done
After catch (will be always executed)
Before try block
Inside try block
Exception CaughtNumber is zero.
Here cleanup activity will be done
After catch (will be always executed)
使用自定义异常类
<?php
class myException extends Exception {
function get_Message() {
// Error message
$errorMsg = 'Error on line '.$this->getLine().
' in '.$this->getFile()
.$this->getMessage().' is number zero';
return $errorMsg;
}
}
function demo($a) {
try {
// Check if
if($a == 0) {
throw new myException($a);
}
}
catch (myException $e) {
// Display custom message
echo $e->get_Message();
}
}
// This will not generate any exception
demo(5);
// It will cause an exception
demo(0);
?>
output:
Error on line 20 in /home/45ae8dc582d50df2790517e912980806.php0 is number zero
设置顶层异常处理程序:set_exception_handler()函数将所有未捕获的异常都设置为所有用户定义的函数。
示例:
<?php
// PHP Program to illustrate normal
// try catch block code
// Function for Uncaught Exception
function myException($exception) {
// Details of Uncaught Exception
echo "\nException: " . $exception->getMessage();
}
// Set Uncaught Exception handler
set_exception_handler('myException');
function demo($var) {
echo " Before try block";
try {
echo "\n Inside try block";
// If var is zero then only if will be executed
if($var == 0)
{
// If var is zero then only exception is thrown
throw new Exception('Number is zero.');
// This line will never be executed
echo "\n After throw (it will never be executed)";
}
}
// Catch block will be executed only
// When Exception has been thrown by try block
catch(Exception $e) {
echo "\n Exception Caught", $e->getMessage();
}
// This line will be executed whether
// Exception has been thrown or not
echo "\n After catch (will be always executed)";
if($var < 0) {
// Uncaught Exception
throw new Exception('Uncaught Exception occurred');
}
}
// Exception will not be raised
demo(5);
// Exception will be raised here
demo(0);
// Uncaught Exception
demo (-3);
?>
output:
Before try block
Inside try block
After catch (will be always executed)
Before try block
Inside try block
Exception CaughtNumber is zero.
After catch (will be always executed)
Before try block
Inside try block
After catch (will be always executed)
Exception: Uncaught Exception occurred