First, error handling
Exception handling: Unexpected, something that happens in the course of a program's operation, using exceptions to change the script's normal flow
A new and important characteristic in PHP5
Copy Code code as follows:
if () {
}else{
}
try {
}catch (Exception object) {
}
1. If there is no problem with the code in the try, execute it after the catch after it has finished executing
2. If the code in try has an exception, throw an exception object (using throw) and throw it to the parameters in the catch, then the code will not continue in the try
Jump directly to the catch to execute, execution complete in the catch, and then continue down execution
NOTE: Hint what happened, this is not the main we have to do things, need to solve this exception in catch, if not resolved, then go out to the user
Ii. Define an exception class yourself
Function: Write one or more methods to solve the process when this exception occurs
1. Define the exception class yourself, it must be a subclass of the exception (built-in Class),
2. Only the construction method and ToString () in the exception class can be overridden, and the others are final
iii. handling multiple exceptions
If you throw an exception in a method when you define a feature class
Copy Code code as follows:
Class Openfileexception extends Exception {
function __construct ($message = null, $code = 0) {
Parent::__construct ($message, $code);
echo "Wwwwwwwwwwwwwww<br>";
}
function open () {
Touch ("Tmp.txt");
$file =fopen ("Tmp.txt", "R");
return $file;
}
}
Class Demoexception extends Exception {
Function Pro () {
echo "Handle the abnormal <br> of demo";
}
}
Class TestException extends Exception {
Function Pro () {
echo "Here deals with the exception of test occurrence <br>";
}
}
Class Helloexception extends Exception {
}
Class MyClass {
function OpenFile () {
$file = @fopen ("Tmp.txt", "R");
if (! $file)
throw new Openfileexception ("File open failed");
}
Function demo ($num =0) {
if ($num ==1)
throw New Demoexception ("demo exception");
}
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<br>";
$my =new MyClass ();
$my->openfile ();
$my->demo (0);
$my->test (0);
$my->fun (1);
echo "22222222222222222<br>";
}catch (openfileexception $e) {//$e =new Exception ();
echo $e->getmessage (). <br> ";
$file = $e->open ();
}catch (Demoexception $e) {
echo $e->getmessage (). <br> ";
$e->pro ();
}catch (TestException $e) {
echo $e->getmessage (). <br> ";
$e->pro ();
}catch (Exception $e) {
echo $e->getmessage (). <br> ";
}
Var_dump ($file);
echo "444444444444444444444<br>";