
- 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 Nested Exception
Introduction
Blocks of try - catch can be nested upto any desired levels. Exceptions will be handled in reverse order of appearance i.e. innermost exception processing is done first.
Example
In following example,inner try block checks if either of two varibles are non-numeric, nd if so, throws a user defined exception. Outer try block throws DivisionByZeroError if denominator is 0. Otherwise division of two numbers is displayed.
Example
<?php class myException extends Exception{ function message(){ return "error : " . $this->getMessage() . " in line no " . $this->getLine(); } } $x=10; $y=0; try{ if (is_numeric($x)==FALSE || is_numeric($y)==FALSE) throw new myException("Non numeric data"); } catch (myException $m){ echo $m->message(); return; } if ($y==0) throw new DivisionByZeroError ("Division by 0"); echo $x/$y; } catch (DivisionByZeroError $e){ echo $e->getMessage() ."in line no " . $e->getLine(); } ?>
Output
Following output is displayed
Division by 0 in line no 19
Change any one of varibles to non-numeric value
error : Non numeric data in line no 20
If both variables are numbers, their division is printed
- Related Articles
- Nested try blocks in Exception Handling in Java
- PHP Exception Handling with finally
- What is Exception Handling in PHP ?
- Create nested JSON object in PHP?
- PHP Exception Handling with Multiple catch blocks
- Differentiate between exception and error in PHP
- How the Non-Capturing Exception Catches work in PHP 8?
- Display a two-dimensional array with two different nested loops in matrix form PHP?
- Difference between system level exception and Application level exception.
- How to resolve exception Element Not Interactable Exception in Selenium?
- Exception in C#
- How to create a user defined exception (custom exception) in java?
- C# Nested Classes
- Is it possible to throw exception without using "throws Exception" in java?
- Python Exception Base Classes

Advertisements