Search code examples
phpfileexceptionspl

Best SPL Exception for file errors in PHP


I want to check if some directory is writable (it should be), and, if not, to throw a technical Exception like this:

if (!is_writable($new_dir)) {
  throw new SomeSPLException ("Bad!");
}

What type of standard PHP SPL Exception best suits in this case?


Solution

  • After taking a look at the built in exceptions

    http://php.net/manual/en/spl.exceptions.php and http://php.net/manual/en/reserved.exceptions.php

    I would personally create my own as none really seem to fit

    class NonWritableDirectoryException extends \Exception {}
    

    Or maybe.. after a second glance.. I'd go for a RuntimeException and pass in a message such as "Unwritable directory", though I still think the first idea is the way to go