Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When would you throw a DomainException in PHP?

Tags:

I was going through the list of predefined Exceptions in PHP and I noticed the DomainException. Anyone know what does DomainException mean? Does it mean failed data model validation?

like image 454
oscarkuo Avatar asked Jul 09 '09 10:07

oscarkuo


People also ask

What is Domainexception?

Exception thrown if a value does not adhere to a defined valid data domain.

What is throw Exception PHP?

The throw keyword is used to throw exceptions. Exceptions are a way to change the program flow if an unexpected situation arises, such as invalid data. The try... catch... finally structure can be used to handle exceptions.

Does throw return PHP?

The thrown object must be an instance of the Exception class or a subclass of Exception. Trying to throw an object that is not will result in a PHP Fatal Error.


1 Answers

There's a pretty hilarious discussion here about how no one seems to know what is means:

http://bugs.php.net/bug.php?id=47097

From the end of that link:

Domain means data domain here. That is a DomainException shall be thrown whenever a value does not adhere to a defined valid data domain. Examples:

  • 0 is not in the domain for division.
  • Foo is not in the domain for weekdays.

The first is different from out of range and alike, but you could use InvalidParameter in case it is actually a parameter to the function that performs the division. If it is a value calculated inside the function prior to executing the division and then a pre-conditon check throws instead of executing the division, then it becomes a DomainException.

like image 121
karim79 Avatar answered Nov 02 '22 23:11

karim79