purencool Posted December 13, 2009 Share Posted December 13, 2009 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 =) Quote Link to comment https://forums.phpfreaks.com/topic/185019-try-catch-error/ Share on other sites More sharing options...
Daniel0 Posted December 13, 2009 Share Posted December 13, 2009 file() doesn't throw exceptions. Quote Link to comment https://forums.phpfreaks.com/topic/185019-try-catch-error/#findComment-976644 Share on other sites More sharing options...
purencool Posted December 13, 2009 Author Share Posted December 13, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/185019-try-catch-error/#findComment-976649 Share on other sites More sharing options...
Daniel0 Posted December 13, 2009 Share Posted December 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185019-try-catch-error/#findComment-976650 Share on other sites More sharing options...
purencool Posted December 13, 2009 Author Share Posted December 13, 2009 Thank you That explains why it won't work=). purencool Quote Link to comment https://forums.phpfreaks.com/topic/185019-try-catch-error/#findComment-976651 Share on other sites More sharing options...
Andy-H Posted December 13, 2009 Share Posted December 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/185019-try-catch-error/#findComment-976654 Share on other sites More sharing options...
Daniel0 Posted December 13, 2009 Share Posted December 13, 2009 Though you shouldn't return an error message from a function call. Instead you should throw an exception or generate an error using trigger_error (exceptions would be preferred). Quote Link to comment https://forums.phpfreaks.com/topic/185019-try-catch-error/#findComment-976658 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.