Nested try catch block in PHP – Exception handling

php Programming Technology

Today, We want to share with you Nested try catch block in PHP – Exception handling.In this post we will show you wordpress plugin require another plugin, hear for Multiple catch blocks and nested try blocks we will give you demo and example for implement.In this post, we will learn about Multiple catch blocks with single try block with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Nested try catch block in PHP – Exception handling

There are the Following The simple About Nested try catch block in PHP – Exception handling Full Information With Example and source code.

As I will cover this Post with live Working example to develop php exception handling best practices, so the php try finally without catch is used for this example is following below.

Simple PHP Example for try-catch

You can also extend the exception class as follows:

php_try_catch.php

<?php
try{
 $products = count($_GET);
 if($products!=0) {
  foreach($_GET as $key=>$value) {
   echo $key . " = " . $value . "<br/>";
  }
 } else {
  throw new Exception("PHP REQUEST GET global array is empty");
 }
}
catch(Exception $e) {
 echo $e->getMessage();
}
?>

PHP Try Catch Exception & Error Handling Tutorial

PHP try with Multiple catch

php_multiple_catch.php

<?php
$current_time_stemp1 = array(1,2,3,"four",5,6);
try{
 $my_timestamp1 = get_timestamp($current_time_stemp1);
 print "PHP time stamp - " . $my_timestamp1 . "<br/>";
}
catch(Exception $e) {
 echo $e->getMessage();
}
catch(InvalidArgumentException $e) {
 echo $e->getMessage();
}

function get_timestamp($current_time_stemp) {
 $current_time_stemp_length = count($current_time_stemp);
 if($current_time_stemp_length!=0) {
  for($i=0;$i<$current_time_stemp_length;$i++) {
   if(!is_numeric($current_time_stemp[$i])) {
    throw new InvalidArgumentException("Please parameter $i should be numeric, " . gettype($current_time_stemp[$i]). " is given");
   }
  }
 } else {
  throw new Exception("Sorry, no Any argument is passed for Date timestmap calculating.");
 }
 $timestamp = mktime($current_time_stemp[0], $current_time_stemp[1], $current_time_stemp[2], $current_time_stemp[3], $current_time_stemp[4], $current_time_stemp[5]);
 return $timestamp; 
}
?>

Nested try-catch in PHP

php_try_catch_nesting.php

//Nested try-catch in PHP
try{
do_upload_file("data.json");
}
catch (Exception $e){
	echo $e->getMessage() . "<br/>";
	while($e = $e->getPrevious()) {
		echo 'Previous Error: '.$e->getMessage() . "<br/>";
	}
}
function do_upload_file( $json_file){
	try{
		if(file_exists($json_file)){
			//PHP File Upload
			echo "Sorry, file already exists.";
			// code for file uploading
		} else {
			throw new Exception( 'file not found');  
		}
	}
	catch (Exception $e){
		throw new Exception( 'Sorry!!, unable to upload file',0,$e);
	}
}

PHP Extending the Exception class

Nested try catch Exception Handaling in PHP (exceptional-php-nesting-exceptions-in-php.php)

class MyException extends Exception{}

try {
 try {
   throw new MyException;
 } catch(MyException $e){
   echo "second:";
   throw $e;
 } catch(Exception $e){
   echo "first:";
   throw $e;
 }
}catch(Exception $e){
	echo get_class($e);
}


echo "<hr/>";

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Nested try catch block in PHP – Exception handling.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Set Cookies Get Cookies Delete Cookies with PHP
  2.   Angular 9/8 Get Selected DropDown Value on Form Submit
  3.   Calculate the difference between two dates using PHP
  4.   Laravel 5.7 Get Current User Details using Controller and blade
  5.   MongoDB CRUD Tutorial With Example From Scratch
  6.   PHP Try Catch Exception & Error Handling Tutorial
  7.   Laravel 5.7 Bootstrap Admin Panel Templates Integrate
  8.   OneDrive Move copy create delete file folder
  9.   Send, Get data redirect route with parameters in laravel
  10.   php try catch Block Exception Handling

Leave a Reply

Your email address will not be published. Required fields are marked *