Home > Backend Development > PHP Tutorial > PHP error and exception handling mechanism (supplementary)_PHP tutorial

PHP error and exception handling mechanism (supplementary)_PHP tutorial

WBOY
Release: 2016-07-21 15:18:23
Original
879 people have browsed it

1. Error handling
Exception handling: Accidents are unexpected things that happen during the running of the program. Use exceptions to change the normal flow of the script
A new important feature in PHP5

Copy code The code is as follows:

if(){
}else{
}
try {
}catch (Exception Object){
}

1. If there is no problem with the code in try, execute the code in try after the catch
2. If an exception occurs in the code in try, an exception object (using throw) will be thrown and given to the parameters in catch. Then the code in try will not continue to execute
and jump directly to catch. Execution, the execution is completed in the catch, and then continues to execute downwards
Note: Prompt what exception occurred, this is not the main thing we need to do, we need to solve the exception in the catch, if it cannot be solved, go out and report it to the user
2. Define an exception class by yourself
Function: It is to write one or more methods to solve the problem of handling this exception when it occurs
1. Define an exception class by yourself, it must be Exception (built-in class) subclass,
2. Only the constructor and toString() in the Exception class can be rewritten, the others are final
3. Handling multiple exceptions
When defining function classes by yourself If an exception is thrown in the method
Copy code The code is as follows:

class OpenFileException extends Exception {
function __construct( $message = null, $code = 0){
parent::__construct($message, $code);
echo "wwwwwwwwwwwwwww
";
}
function open(){
touch("tmp.txt");
$file=fopen("tmp.txt", "r");
return $file;
}
}
class DemoException extends Exception {
function pro(){
echo "Handle exceptions that occur in demo
";
}
}
class TestException extends Exception {
function pro() {
echo "Handle test exceptions here
";
}
}
class HelloException extends Exception {
}
class MyClass {
function openfile( ){
$file=@fopen("tmp.txt", "r");
if(!$file)
throw new OpenFileException("File opening failed");
}
function demo($num=0){
if($num==1)
throw new DemoException("Exception in demonstration");
}
function test($num=0 ){
if($num==1)
throw new TestException("Test error");
}
function fun($num=0){
if($num= =1)
throw new HelloException("###########");
}
}
try{
echo "11111111111111
";
$my=new MyClass();
$my->openfile();
$my->demo(0);
$my->test(0);
$my->fun(1);
echo "22222222222222222
";
}catch(OpenFileException $e){ //$e =new Exception();
echo $e- >getMessage()."
";
$file=$e->open();
}catch(DemoException $e){
echo $e->getMessage( )."
";
$e->pro();
}catch(TestException $e){
echo $e->getMessage()."
";
$e->pro();
}catch(Exception $e){
echo $e->getMessage()."
";
}
var_dump($file);
echo "444444444444444444444
";

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325497.htmlTechArticle1. Error handling Exception handling: Accidents are unexpected things that happen during the running of the program. Use Exception changes the normal flow of the script A new important feature in PHP5 Copy code...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template