Hướng dẫn php catch multiple exceptions
Exceptions are used to change the normal flow of a script if a specified error occurs. Show
Nội dung chính
What is an ExceptionWith PHP 5 came a new object oriented way of dealing with errors. Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception. This is what normally happens when an exception is triggered:
We will show different error handling methods:
Note: Exceptions should only be used with error conditions, and should not be used to jump to another place in the code at a specified point. Basic Use of ExceptionsWhen an exception is thrown, the code following it will not be executed, and PHP will try to find the matching "catch" block. If an exception is not caught, a fatal error will be issued with an "Uncaught Exception" message. Lets try to throw an exception without catching it: //create function with an exception //trigger exception The code above will get an error like this: Fatal error: Uncaught exception 'Exception' Try, throw and catchTo avoid the error from the example above, we need to create the proper code to handle an exception. Proper exception code should include:
Lets try to trigger an exception with valid code: //create function with an exception //trigger exception in a "try" block //catch exception The code above will get an error like this: Message: Value must be 1 or below Example explained:The code above throws an exception and catches it:
However, one way to get around the "every throw must have a catch" rule is to set a top level exception handler to handle errors that slip through. Creating a Custom Exception ClassTo create a custom exception handler you must create a special class with functions that can be called when an exception occurs in PHP. The class must be an extension of the exception class. The custom exception class inherits the properties from PHP's exception class and you can add custom functions to it. Lets create an exception class: class customException extends Exception { $email = ""; try { catch (customException $e) { The new class is a copy of the old exception class with an addition of the errorMessage() function. Since it is a copy of the old class, and it inherits the properties and methods from the old class, we can use the exception class methods like getLine() and getFile() and getMessage(). Example explained:The code above throws an exception and catches it with a custom exception class:
Multiple ExceptionsIt is possible for a script to use multiple exceptions to check for multiple conditions. It is possible to use several if..else blocks, a switch, or nest multiple exceptions. These exceptions can use different exception classes and return different error messages: class customException extends Exception { $email = ""; try { catch (customException $e) { catch(Exception $e) { Example explained:The code above tests two conditions and throws an exception if any of the conditions are not met:
If the exception thrown were of the class customException and there were no customException catch, only the base exception catch, the exception would be handled there. Re-throwing ExceptionsSometimes, when an exception is thrown, you may wish to handle it differently than the standard way. It is possible to throw an exception a second time within a "catch" block. A script should hide system errors from users. System errors may be important for the coder, but are of no interest to the user. To make things easier for the user you can re-throw the exception with a user friendly message: class customException extends Exception { $email = ""; try { catch (customException $e) { Example explained:The code above tests if the email-address contains the string "example" in it, if it does, the exception is re-thrown:
If the exception is not caught in its current "try" block, it will search for a catch block on "higher levels". Set a Top Level Exception HandlerThe function myException($exception) { set_exception_handler('myException'); throw new Exception('Uncaught Exception occurred'); The output of the code above should be something like this: Exception: Uncaught Exception occurred In the
code above there was no "catch" block. Instead, the top level exception handler triggered. This function should be used to catch uncaught exceptions. Rules for exceptions
A simple rule: If you throw something, you have to catch it. How can you handle multiple exception in PHP?Rules for exceptions. Code may be surrounded in a try block, to help catch potential exceptions.. Each try block or "throw" must have at least one corresponding catch block.. Multiple catch blocks can be used to catch different classes of exceptions.. Exceptions can be thrown (or re-thrown) in a catch block within a try block.. What can we do to handle multiple exceptions?Java Catch Multiple Exceptions A try block can be followed by one or more catch blocks. Each catch block must contain a different exception handler. So, if you have to perform different tasks at the occurrence of different exceptions, use java multi-catch block. Can we use multiple try catch in PHP?PHP allows a series of catch blocks following a try block to handle different exception cases. Various catch blocks may be employed to handle predefined exceptions and errors as well as user defined exceptions. What is exceptional handling in PHP?Exception handling is a powerful mechanism of PHP, which is used to handle runtime errors (runtime errors are called exceptions). So that the normal flow of the application can be maintained. The main purpose of using exception handling is to maintain the normal execution of the application. |