catch try php
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
return 1/$x;
}Try catch comes under exception handeling concept where using this we control the runtime error and modify the message as we want.
// function created with exception throw
function checkdata($number){
if($number > 10){
throw new Exception("Number is greater than 10");
}
return true;
}
// try block starts
try{
checkdata(15);
echo "The number is below 10";
}
// catch block
catch(Exception $e){
echo "Message :".$e->getMessage();
}
In above code if condition is not satisfied then it will throw exception and which gets caught by catch block and show the error message.
Also in PHP:
- cache clear in laravel command
- PackageManifest.php line 131: Undefined index: name
- find array duplicates in two arrays php
- Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 7.3.0". php 8
- Create Laravel Project 7
- can php date be echoed as string
- create seede for table in laravel
- clear cache automatically laravel
- array_reverse function in php
- Allowed memory size of 1610612736 bytes exhausted (tried to allocate 40 bytes) in phar:///opt/cpanel/composer/bin/composer/src/Composer/DependencyResolver/RuleSetGenerator.php on line 129
- check substring in php
- api newslater with php
- array_reverse in php
- codeigniter get lst inserted id
- corsair k70 rgb mk.2
- check if a key is in an array
- count string char php
- different elements of array php
- centos 7 install php composer
- composer laravel 7
- find string length php
- can i back up mysql database from php code?
- chow to check which php version running wordpress
- Str in laravel