
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Exception Handling with Multiple catch blocks
Introduction
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.
Example
Following example uses catch blocks to process DivisioByZeroError, TypeError, ArgumentCountError and InvalidArgumentException conditions. There is also a catch block to handle general Exception.
Example
<?php declare(strict_types=1); function divide(int $a, int $b) : int { return $a / $b; } $a=10; $b=0; try{ if (!$b) { throw new DivisionByZeroError('Division by zero.');} if (is_int($a)==FALSE || is_int($b)==FALSE) throw new InvalidArgumentException("Invalid type of arguments"); $result=divide($a, $b); echo $result; } catch (TypeError $x)//if argument types not matching{ echo $x->getMessage(); } catch (DivisionByZeroError $y) //if denominator is 0{ echo $y->getMessage(); } catch (ArgumentCountError $z) //if no.of arguments not equal to 2{ echo $z->getMessage(); } catch (InvalidArgumentException $i) //if argument types not matching{ echo $i->getMessage(); } catch (Exception $ex) // any uncaught exception{ echo $ex->getMessage(); } ?>
Output
To begin with, since denominator is 0, Divide by 0 error will be displayed
Division by 0
Set $b=3 which will cause TypeError because divide function is expected to return integer but dividion results in float
Return value of divide() must be of the type integer, float returned
If just one variable is passed to divide function by changing $res=divide($a); this will result ArgumentCountError
Too few arguments to function divide(), 1 passed in C:\xampp\php\test1.php on line 13 and exactly 2 expected
If one of arguments is not integer, it is a case of InvalidArgumentException
Invalid type of arguments
- Related Articles
- Exception Hierarchy in case of multiple catch blocks.
- PHP Exception Handling with finally
- Nested try blocks in Exception Handling in Java
- What is Exception Handling in PHP ?
- Can we define a try block with multiple catch blocks in Java?
- Can a try block have multiple catch blocks in Java?
- Is it possible to have multiple try blocks with only one catch block in java?
- Exception handling with method overriding in Java.
- Exception handling in PowerShell
- What are unreachable catch blocks in Java?
- Exception Handling Basics in C++
- C# Exception Handling Best Practices
- How to use Try/catch blocks in C#?
- What are try, catch, finally blocks in Java?
- Exception Handling in C++ vs Java
