catch error 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:
- check if string has comma php
- Your Composer dependencies require the following PHP extensions to be installed: fileinfo
- clear laravel cache and clear vue
- check result statusCode amazon sms api
- cacert file_get_contents openssl
- Deleting an element from an array in PHP
- find index of occurrence php
- 'PHP message: PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 282624 bytes)
- delete composer for laravel
- date chenge format php
- Y-m-dTH:
- data type php check
- file upload in php through ajax
- -5 php change timezone
- PDO encode result recordset to utf8
- enable error reporting in php
- disable register route laravel
- ->with('success','msj')
- PHP text string contains character
- check sting contsin sub sting php
- current uri php[
- 'cross-env' is not recognized as an internal or external command, laravel
- check if string contains substring pho
- check if specific index of array exist php