When should I use Exception, InvalidArgumentException or UnexpectedValueException?
I don't know the real different between them as I always used Exception.
Different exceptions just give you more granularity and control over how you catch and handle exceptions.
Consider a class where you are doing many things - e.g. getting input data, validating input data and then saving it somewhere. You might decide that if the wrong or empty arguments are passed to the get()
method, you might throw an InvalidArgumentException
. When validating, if something is out of the ordinary or doesn't match up you could throw an UnexpectedValueException
. If something totally unexpected happens you could throw a standard Exception
.
This becomes useful when you are catching, as you can handle different types of exceptions in different ways. For example:
class Example
{
public function get($requiredVar = '')
{
if (empty($requiredVar)) {
throw new InvalidArgumentException('Required var is empty.');
}
$this->validate($requiredVar);
return $this->process($requiredVar);
}
public function validate($var = '')
{
if (strlen($var) !== 12) {
throw new UnexpectedValueException('Var should be 12 characters long.');
}
return true;
}
public function process($var)
{
// ... do something. Assuming it fails, an Exception is thrown
throw new Exception('Something unexpected happened');
}
}
In the above example class, when calling it you could catch
multiple types of exceptions like so:
try {
$example = new Example;
$example->get('hello world');
} catch (InvalidArgumentException $e) {
var_dump('You forgot to pass a parameter! Exception: ' . $e->getMessage());
} catch (UnexpectedValueException $e) {
var_dump('The value you passed didn\'t match the schema... Exception: ' . $e->getMessage());
} catch (Exception $e) {
var_dump('Something went wrong... Message: ' . $e->getMessage());
}
In this case you get an UnexpectedValueException
like this: string(92) "The value you passed didn't match the schema... Exception: Var should be 12 characters long."
.
It should also be noted that these exception classes all end up extending from Exception anyway, so if you don't define special handlers for the InvalidArgumentException
or others then they will be caught by Exception
catchers anyway. So really, why not use them?