Jump to content

[SOLVED] Why use a custom Exception?


ifubad

Recommended Posts

Got the following first custom Exception example from w3schools. The second example is a slightly modified version, that is not using a custom Exception, and outputs exactly the same message, with slightly less code overall. As stated by somewhere explaining why use a custom exception, because "The default exception class doesn't exactly fulfill the graceful part of handling unpredictable results. It just prints out an error message not much different from regular errors."

 

So, what is the big advantage of using a custom exception, if the more graceful message can be printed out within the echo statement of the catch block, when using the default Exception class. To produce cleaner code?

 

Using custom Exception

<?php
class customException extends Exception
  {
  public function errorMessage()
    {
    //error message
    $errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
    .': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';
    return $errorMsg;
    }
  }

$email = "someone@example...com";

try
  {
  //check if
  if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
    {
    //throw exception if email is not valid
    throw new customException($email);
    }
  }

catch (customException $e)
  {
  //display custom message
  echo $e->errorMessage();
  }
?>

 

Using default Exception

<?php
$email = "someone@example...com";

try
  {
  //check if
  if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
    {
    //throw exception if email is not valid
    throw new Exception($email);
    }
  }

catch (Exception $e)
  {
  //display custom message
  echo 'Error on line '.$e->getLine().' in '.$e->getFile()
    .': <b>'.$e->getMessage().'</b> is not a valid E-Mail address';
  }
?>

Link to comment
Share on other sites

Because (as with any customized class) a customized exception may include mechanisms for logging, emailing admins, all sorts of cool stuff. You may also want to try and catch different types of exceptions. eg;

 

try {
  validate_form();
} catch (envalidNameException $e) {
  // handle invalid name.
} catch (envalidEmailException $e) {
  // handle invalid email.
} catch (envalidPassException $e) {
  // handle invalid password.
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.