I am trying to figure out the need of try block in exception handling.
I am learning custom error handling in php and the code is as follows:
class customException extends Exception{
public function errorMessage(){
return "Error at line ".$this->getLine()." in ".$this->getFile()."<br>".$this->getMessage()." is not a valid email address";
}
}
$email="[email protected]";
try{
if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
throw new customException($email);
}
}
catch(customException $e){
echo $e->errorMessage();
}
The code being executed in the try
block may throw different types of exceptions
try {
thingThatMightBreak();
}
catch (CustomException $e) {
echo "Caught CustomException ('{$e->getMessage()}')\n{$e}\n";
}
catch (Exception $e) {
echo "Caught Exception ('{$e->getMessage()}')\n{$e}\n";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With