How to use Exception handling?
Exception handling was introduced in PHP 5 as a new way for the execution of code and what exceptions to make in code in case of a specific error.
There are various keywords of exception handling in PHP like:
Try: This try block consists of the code which could potentially throw an exception. All the code in this try block is executed unless and until an exception is thrown.
Throw: This throw keyword is used to alert the user of a PHP exception occurrence. The runtime will then use the catch statement to handle the exception.
Catch: This keyword is called only when there is an exception in the try block. The code of catch must be such that the exception thrown is handled efficiently.
Finally: This keyword is used in PHP versions 5.5 and above where the code runs only after the try and catch blocks have been executed. The final keyword is useful for situations where a database needs to be closed regardless of the occurrence of an exception and whether it was handled or not.
Note: From the vast number of PHP interview questions, answering this will help you to align yourself as an expert in the subject. This is a very tricky question that often makes developers nervous.