exceptions in 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:
- How to set a comment on table using Laravel Schema
- date(t) php
- date + 1 php
- In PackageManifest.php line 131: Undefined index: name laravel 7
- clear cache config laravel
- date manual php
- .length php
- eloquent remove updated_at
- SQLSTATE[42S02] lumen
- cache clear laravel command
- Best IDE for php
- does php send a new page
- check to see in string contains a specifc word php
- class name laravel
- array_key_exists vs isset
- To find out where your php.ini is located
- all words capital php
- convert date in php
- file_get_contents() [function.file-get-contents]: SSL operation failed with code 1. OpenSSL Error messages
- check if string contains substring pho
- Date - hours php
- check if home page wordpress
- composer and php version
- Laravel users_email_unique