NYCPHP Meetup

NYPHP.org

[nycphp-talk] If/else vs Try/catch

Daniel Convissor danielc at analysisandsolutions.com
Wed Nov 28 20:48:02 EST 2007


On Wed, Nov 28, 2007 at 12:53:21PM -0500, Kenneth Downs wrote:
> 
> function try_include($filename) {
>    try {
>       include($filename);
>    }
>    catch Exception(e) {
>       echo "Problem trying to include file!";
>       return false;
>    }
>    return true;
> }

Huh?  include(), and most of PHP's native procedural functions, does not 
throw an exception.  So the try/catch in your example does nothing.

You _could_ do something along these lines, though...

function try_include($file) {
    ini_set('track_errors', 1);
    $php_errormsg = '';
    $result = @include($file);
    if ($php_errormsg != '') {
        throw new Exception('Problem including ' . $file);
    }
    return $result;
}

try
{
    try_include('exists.inc');
    try_include('does_not_exist.inc');
}
catch (Exception $e)
{
    echo $e->getMessage();
    exit;
}


--Dan

-- 
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
            data intensive web and database programming
                http://www.AnalysisAndSolutions.com/
 4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409



More information about the talk mailing list