PHP try-catchPHP try and catch are the blocks with the feature of exception handling, which contain the code to handle exceptions. They play an important role in exception handling. There is one more important keyword used with the try-catch block is throw. The throw is a keyword that is used to throw an exception. Each try block must have at least one catch block. On the other hand, a try block can also have multiple catch blocks to handle various classes of exception. SyntaxThe following syntax is used in exception handling to handle runtime errors - Let's learn about try-throw-catch in more detail: tryThe try block contains the code that may contain an exception. An exception raised in try block during runtime is caught by the catch block. Therefore, each try block must have at least one catch block. It consists of the block of code in which an exception can occur. Following points needs to be noted about the try:
catchThe catch block catches the exception raised in the try block. It contains the code to catch the exception, which is thrown by throw keyword in the try block. The catch block executes when a specific exception is thrown. PHP looks for the matching catch block and assigns the exception object to a variable. Following points to be noted about the catch:
throwIt is a keyword, which is used to throw an exception. Note that one throw at least has one "catch block" to catch the exception. It lists the exceptions thrown by function, which cannot be handled by the function itself. finallyIt is a block that contains the essential code of the program to execute. The finally block is also used for clean-up activity in PHP. It is similar to the catch block, which is used to handle exception. The only difference is that it always executes whether an exception is handled or not. The finally block can be specified after or in place of catch block. It always executes just after the try and catch block whether an exception has been thrown or not, and before the normal execution restarts. It is useful in the following scenarios - Closing of database connection, stream. Example 1Let's take an example to explain the common flow of throw and try-catch as well as finally block: Output: Exception Message: Value must be less than 1 It is finally block, which always executes. Example 2Output: In the below output, you can see that when an odd number is passed, an exception message is displayed. On the other hand, when an even number is passed, another message is shown. Output for ODD Number Exception Message: Passed number is an ODD Number It is finally block, which always executes. Output for EVEN Number If you see this text, the passed value is an EVEN Number It is finally block, which always executes. Example 2: Custom Exception and Multiple catch statementIn the below example, we will create a custom exception class that extends the Exception class. Here, we will also use multiple catch blocks with a single try block. Output: Output for EVEN Number If you see this text, the passed value is an EVEN Number Output for ODD Number Exception Message: ODD Number Important Points:
Next TopicPHP Pagination
|