How to deal with violations of regulations in other places? Supplementary PHP error and exception handling mechanisms

WBOY
Release: 2016-07-29 08:48:33
Original
928 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 the code The code is as follows:


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


1. If there is no problem with the code in try, then the code in try will be executed after the catch Execution
2. If an exception occurs in the code in try, throw an exception object (using throw) and throw it to the parameters in catch, then the code in try will not continue to execute
Jump directly to catch Execute in catch, execution is completed in catch, and then continue to execute. Note: Prompt what exception occurred. This is not the main thing we need to do. We need to solve this exception in catch. If it cannot be solved, go out and report it to the user. 2. Yourself Define an exception class
Function: It is to write one or more methods to solve the problem of handling this exception when it occurs
1. Define your own exception class, which must be a subclass of Exception (built-in class),
2. The only exceptions in the Exception class The constructor and toString() can be rewritten, and the others are final
3. Handle multiple exceptions
If an exception is thrown in the method when defining the function class by yourself


Copy the 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 demo Exception that occurred
";
}
}
class TestException extends Exception {
function pro(){
echo "Exceptions that occurred in test are handled 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 occurred in the 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 "44444444444444444444
" ;


The above introduces how to deal with violations in different places. The PHP error and exception handling mechanism is supplemented, including how to deal with violations in different places. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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