the exception handling with example in 2021 |
PHP exception handling with example in 2021
PHP Exception: To understand the exception firstly we should know about the error in the program. Errors in programs can be categorized into two types.
1. Compile Time Error
2. Run Time Error
Compile Time Errors: Errors
caught during the compiled time are called Compile-time errors. Compile-time errors
include library reference, syntax error, or incorrect class import.
Run-Time Errors: The error that occurs during the run time of the program is called run time error. They are also known as exceptions. Hence we can say that exception is a runtime error that occurs mostly because of a user's mistake.
1. Exception is a problem that interrupts the normal flow of
execution of the program.
2. Exception Handling in PHP is almost similar to exception
handling in all programming languages.
Reasons of Exception
Mismatched Input: Suppose that we are entering our name in place of age, causing exception because age is of data type int and name will be a string.
File does not exist: Suppose that we are reading a text file easy.txt and that file does not exist in the system, causing the exception.
Divide by zero exception: When a number is divided by zero then the output will be undefined(infinity).
Exception Handling: The process of handling the exception is called
exception handling. There are four main keywords are used to solve the problem
of exception.
try block: It is the place where actual code is written
and exceptions occur. when the code will lead to an error, that
error/exception will get caught inside the catch block. Each try must have at
least one corresponding catch block.
catch block: catch block is intended to catch the error and
handle the exception condition. We can have multiple catch blocks to handle
different types of exceptions and perform different actions when the exceptions
occur. Multiple catch blocks can be used to catch different classes of
exceptions.
throw statement: It is used to throw an exception. It is used to
show the user-defined message about the exception.
finally, block: It is used in place of catch block or after the catch block. This block executes either exception occurs or does not occur.
Exception Syntax of
try-catch
Try { // protected code } catch( ExceptionName e1 ) { // catch block } catch( ExceptionName e2 ) { // catch block } catch( ExceptionName e n ) { // catch block } |
How To use Exception try-catch in PHP?
<?php try { $err = "This is error message"; throw new Exception($err); echo "This statement will not execute"; } catch (Exception $e) { echo "Caught
exception:".$e->getMessage()."<br>"; } echo "Hello, How are you?"; ?> *****OUTPUT***** Caught exception: This is the error message Hello Programmer, Welcome To Php language |
From the above example, we can see that the throw keyword is used to
throw the exception, this error message is caught by the catch block.
The code following an exception will not execute.
How To use Exception try-catch in php?
<?php function divide($x,$y) { try { if($y==0) { $err = "don't put zero denominator"; throw new Exception($err); } else { $z=$x/$y; echo "Division:".$z."<br>"; } } catch (Exception $e) { echo "Caught
exception:".$e->getMessage()."<br>"; } } divide(120,5); divide(120,0); ?> *****OUTPUT***** Division:24 Caught exception: don't put zero denominator |