PHP error, exception handling mechanism (supplemental) _php Tips

Source: Internet
Author: User
Tags exception handling getmessage php error
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>";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.