exception php try catch
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:
- How do I get a YouTube video thumbnail from the YouTube API?
- Parse error</b>: syntax error, unexpected ':', expecting ';' or '{' in <b>//twilio-php-master/src/Twilio/autoload.php</b> on line <b>61</b><br />
- ctrl + d in vscode in phpstorm
- datetime php add one day to today
- PHP Password Generator
- < Php echo date("m") . " "; ?> what is the output of this script?
- ::time in php
- Syntax error or access violation: 1071 Specified key was too long
- extract($user_input); alternative php
- In PackageManifest.php line 131: Undefined index: name laravel 7
- base64_decode online php
- date('hms'
- PHP header(refresh)
- PDOException::("could not find driver") windows
- check if number doesn't divide exactly by 3 php
- check if string contains certain word
- check current datetime in php
- array hass a key in php
- does php send a new page
- clear server cache laravel
- convert string date to timestamp php
- date now php
- changing a part of a string into uppercase php
- 10 digit random number generator php