The magic __toString() method will be invoked automatically when the code enters the catch()statement and the custom exception message will be printed
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:
- base64 decode using php
- catch custom exception php
- composer create project laravel 7
- current URL without url site laravel
- authenticate user with phone laravel
- check array for duplicate values php
- Artisan call Cache:clear force
- check if substr exist php
- $_Server php find
- composer laravel install
- factory laravel seeder
- check if index is undefined php
- check array key e\
- check if its index page woocmerce
- carbon format date in laravel
- check if array has only the given key on php
- delete duplicate elements array php
- clear laravel cash
- <?php echo do_shortcode(' '); ?> with variable
- echo errors of php
- To find out where your php.ini is located
- create model controller migration factory laravel in one command
- create project laravel
- add another field in existing migration laravel