Mathew Hany
Mathew Hany

Reputation: 14060

What's the exception that should be used?

I began to use exceptions in programming since soon, but my problem is: I don't know what's the exception that should be used, actually I know when to use InvalidArgumentException, but how about RuntimeException and others? I read PHP Manual, but I still do not know when to use each exception.

This is my main problem now, I want to know when to use each exception, and why to use exceptions?!

Upvotes: 0

Views: 47

Answers (1)

GolezTrol
GolezTrol

Reputation: 116110

Use exception or not

The discussion whether to use exceptions or not is a tough one. Some use exceptions a lot, almost like control flow statements. Every time when an unexpected condition occurs, an exception is thrown. For instance, you can build a function that verifies an e-mail address and throws an exception when it fails. Your code might look like this:

// This is declared somewhere in a central place.
class VerificationException extends RuntimeException
{
}

// This is the code in your form validation process.
try
{
  verifyName($_POST['email']);
  verifyEmail($_POST['email']);
  verifyPhoneNumber($_POST['email']);
}
catch (VerificationException $e)
{
  // Show the error to the user here.
}
catch (Exception $e)
{
  // Any other error. Log it, let the script fail... you decide.
}

Others find that you should not use exceptions at all. They say, you should just return false or some error code, and don't use exceptions, since they make your code harder to debug. So the function verifyEmail should just return false in their book. You code would be something like this:

if (verifyName($_POST['email']) === false) {
  // Handle this failed verification.
}
if (verifyEmail($_POST['email']) === false) {
  // Handle this failed verification.
}
if (verifyPhoneNumber($_POST['email']) === false {
  // Handle this failed verification.
}

I won't step into the discussion which is better, (which is not suitable for SO anyway), but let me say that I'm somewhere in between. The difference is that an exception just bubbles to a higher level if it is not caught. If you don't store the result of a function, that result is just gone, but an exception can be caught in a calling function, or in the caller of the caller, or even on a very high level in the script. This allows for more flexibility, but also makes the structure of the program harder to follow.

PHP's built in Exception classes and custom exception classes

PHP provides a couple of exception classes (see reserved exceptions and SPL exceptions). Each of them has a small description that tells you its purpose. But you can also define your own exception classes that either extend from one of the aforementioned classes, or from Exception itself.

RuntimeException is actually a base class for exceptions that can occur at runtime. Rather than using that one, you can better use one of its specific child classes, like UnexpectedValueException.

If you do use exceptions, you have probably found that in the except clause of a try..except block, you can specify which class of exception you want to catch (see also my code sample above). So if you throw specific exception type, you can also be more specific in catching them, without having to parse actual error messages.

The main purpose for the different exception classes is having a clear way to distinguish them, so you you should just check the list and find the one that feels most suitable to you. In addition, you can extend an exception class and add new functionality, like a specific error code or other meta information that is not part of the exception message but might still be useful for the one handling the exception.

Upvotes: 1

Related Questions