Tuesday, 9 June 2020

how to generate custome error message

how to generate custome error message
<?php

$a = 'apple';
if ($a == 'apple'){
echo 'YES';
} else {
throw new Exception;
inbuilt class throw to generate errors
}
?>



$age = 18;
try {
if try failed, go to else, it has throw , it will call catch, at that it wil execute
throw
whenever else comes comes to thtrow
}
and execute in catch
catch() {

}
<?php
$age = 18;
try {
if ($age  >= 18){

echo ' you can vote';
if it is not age is equalto 18
} else {
throw the error as exception with text
throw new Exception ('YOU CANT ENTER');
whats we given text
}
that will catched by catch with
catch (Exception $ex){
echo $ex -> getmessage();
automatically get by the message function and loads to ex and prints
}


?>

how to check it is age below the age , u cant enter
condional enter and generate error



No comments:

Post a Comment