How to re-throw exception in PHP

1 Answer

0 votes
/*
throw new Exception($error_message);
*/

class customExceptionClass extends Exception 
{
  public function errorMessage() 
  {
        $errorMsg = 'Error on line: ' . $this->getLine() . "<br />" . 
                    'in file ' . $this->getFile() . "<br />" .
                    'Error Message: ' . $this->getMessage() . "<br />";
        return $errorMsg;
  }
}

$email = "test@email.com";

try 
{
  try 
  {
    if (strpos($email, "test") !== FALSE) {
        throw new Exception($email);
    }
  }
  catch(Exception $e) 
  {
    // re-throw exception
    throw new customExceptionClass("email " . $email . " is not valid");
  }
}

catch (customExceptionClass $e) 
{
  echo $e->errorMessage();
}

        
/*
run:

Error on line: 30
in file C:\xampp\htdocs\workingframe.com\test.php
Error Message: email test@email.com is not valid
  
*/

 



answered Jan 4, 2016 by avibootz

Related questions

1 answer 243 views
243 views asked Nov 27, 2020 by avibootz
1 answer 294 views
294 views asked Jan 3, 2016 by avibootz
1 answer 283 views
2 answers 270 views
270 views asked Oct 21, 2022 by avibootz
1 answer 244 views
1 answer 238 views
238 views asked Nov 27, 2020 by avibootz
...