Search code examples
exceptionphp-5.3php

Trying to throw an exception gives fatal error with php cli


I'm trying to throw new Exception(...) in a script written for PHP CLI.

This is the error:

PHP Fatal error:  require(): Failed opening required '/Exception.php'
(include_path='.:/usr/share/php:/usr/share/pear') in /file.php

This is the line that refers to:

if (!file_exists($path)) {
    if (!mkdir($path)) {
        throw new Exception('Unable to create directory: ' . $path);
    }
}

Is this a config issue or is something else happening to cause this?

I've not written anything before to be used with PHP CLI so not sure on a lot of things like this.

Thanks for all help.


Solution

  • This error has nothing to do with php cli. Also it has nothing to do with Exceptions.

    The message error you receive is due to the fact that in your code you have a sentence like:

    <?php
    
    require '/Exception.php';
    

    And the file Exception.php doesn't exists in your root.

    For using php Exceptions you don't need to include any require. If you remove the above sentence, your error code will desapear.