exception handling in php with example
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:
- composer uninstall package
- create laravel migration
- array_key_exists not finding numbers
- Install or enable PHP's gd extension
- convert timestamp php
- check if string contains specific string php
- echo gettype
- check to see in string contains a specifc word php
- create php laravel project
- fgets php
- check all file in server php
- date U
- clear cache manually laravel
- Y-m-d H:s:i input
- PHP STRING LENGT
- check if word belongs in string
- check string contain php
- What parameter should you enable if you wish for your browser to show all errors?
- SET DEFAULT hide title astra wordpress
- Class 'Intervention\Image\ImageServiceProvider' not found
- alert in php
- <?php echo Date(Y); ?>
- convert lowercase to uppercase php
- C:\MyScans in php server run file