Jump to content

Recommended Posts

hi php lovers

 

I have been trying  try catch but, I can't get it to work. The code opens a non existing file called "a"

 

private function fileOpenMethod($filePath, $numberOfLines,$header,$replaceHeader){
       try{
        $file = file( $filePath);
       }catch(file $e){
           $e= "the flie path does not work"; //. $e->getMessage();
       }

return $e;
}

I am getting an error message on the  page but not the one that am trying to generate

Warning: file(a) [function.file]: failed to open stream: No such file or directory in /home/public_html/_Doc/StripFileClass.php on line 15

 

I am sure it is simple but I am new to try and catch and it is a learning curve blah blah blah.

Can anyone see what I am doing wrong =)

Link to comment
https://forums.phpfreaks.com/topic/185019-try-catch-error/
Share on other sites

Thanks for the reply

 

Your answer as create a couple of questions

1.I know that i can suppress the error using @ is there a way to force the error into a variable at all?

2.Is try and catch only  going to work on my code not the functions called inside the code I write?

 

 

Link to comment
https://forums.phpfreaks.com/topic/185019-try-catch-error/#findComment-976649
Share on other sites

1.I know that i can suppress the error using @ is there a way to force the error into a variable at all?

 

If you turn on track_errors in php.ini you can get the last error/warning that is generated in $php_errmsg. I don't think it'll work if you suppress the error though. Generally speaking, you shouldn't suppress errors, however.

 

2.Is try and catch only  going to work on my code not the functions called inside the code I write?

 

A try-catch block works by transferring control to the catch block if a designated exception is thrown within the try block. If no exception occurs, the catch block will not be executed.

 

An error is not the same as an exception.

Link to comment
https://forums.phpfreaks.com/topic/185019-try-catch-error/#findComment-976650
Share on other sites

private function fileOpenMethod($filePath, $numberOfLines, $header, $replaceHeader)
{
    try
    {
        $file = file($filePath);
        if ($file === false || empty($file))
            throw new exception("The file path is invalid or doesn't exist.\n");
    }
    catch(Exception $e)
    {
        return $e->getMessage();
    }

return $file;
}

 

file try exception

Link to comment
https://forums.phpfreaks.com/topic/185019-try-catch-error/#findComment-976654
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.