Hướng dẫn php catch database error
Tác giả: Dương Nguyễn Phú Cường Show Ngày đăng: Hồi xưa đó What is an Exception?An error is an unexpected program result that cannot be handled by the program itself. Errors are resolved by fixing the program. An example of an error would be an infinite loop that never stops executing. An exception is unexpected program result that can be handled by the program itself. Examples of exception include trying to open a file that does not exist. This exception can be handled by either creating the file or presenting the user with an option of searching for the file. Why handle exception?
When an error occurs, depending on your configuration settings, PHP displays the error message in the web browser with information relating to the error that occurred. PHP offers a number of ways to handle errors. We are going to look at three (3) commonly used methods;
Error handling examplesLet’s now look at some simple examples with error handling routines. Let’s suppose that we have developed an application that uses text files to store data. We might want to check for the file’s existence before we attempt to read data from it. The code below implements the above example. Assuming you saved the file simple_error.php in phptuts folder, open the URL http://localhost/phptuts/simple_error.php You will get the following results Assuming you saved the above code as error_handling.php, open the URL http://localhost/phptuts/error_handling.php The custom error handler will be set as the default PHP error handling function and will basically display an error number and message. The code below illustrates the implementation of the above example Open the URL http://localhost/phptuts/custom_error_handler.php you will get the following results .
Let’s now look at the third type of error handling. We will be using the PHP built in function error_reporting function. It has the following basic syntax HERE,
Difference between Errors and Exception
The table below shows the exception object methods
Below is the basic syntax for throwing an exception. HERE,
The above code outputs the following message. HERE,
The code below shows the basic exception example with the try, throw and catch exception implemented. The program deliberately throws an exception which it then catches. getMessage(); echo ""; echo "getCode(): " . $e->getCode(); echo ""; echo "__toString(): " . $e->__toString(); } ?> Open the URL http://localhost/phptuts/exception_handling.php You will get the following results. Multiple ExceptionsMultiple exception use multiple try catch blocks to handle the thrown exceptions. Multiple exceptions are useful when;
The flowchart below illustrates the how multiple exceptions work We expect two types of exceptions to occur;
For simplicity’s sake, we will only display the exception type in our catch blocks. The PHP built in Exception class is used to throw exceptions. We will create two classes that extend the exception class and use them to throw exceptions. The code below shows the implementation. Testing the codeWe will assume you saved multiple_exceptions.php in phptuts folder. Browse to the URL http://localhost/phptuts/multiple_exceptions.php Summary
|