Get error information

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Get error information

Post by alex.barylski »

Ok Feyd, trick question...

I need to detect when a include_* has been called and failed because file didn't exist...but instead of choking the script...I need to be able to get the name of the path of the file requested programatically, without parsing output errors...

As that obviously would be a bad way of getting the path requested...cuz i'm sure that changes frequently, language, etc...

Possible?

p.s-This is not a situation where I can use file_exists() I need the script to execute, fail throwing a warning and then be able to get the path from that warning of the requested file...

I've considered using the buffer functions and extracting the suggested path, but that doesn't seem fitting as the message could likely change drastically from one machine to another...???

I've looked for a get last error type funciton and couldn't find anything??? :(
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

I though this maybe possible with try/catch but I can't seem to find anything on it???

Doesn't PHP5 support try/catch? :?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I think what your after is exception handling.

Code: Select all

try {
   $includePath = 'path/to/file.txt';
   
   if (!@include($includePath)) {
      throw new Exception('Cannot find file '. $includePath);
   }

   echo 'File included fine!';

} catch (Exception $e) {
   echo 'Caught exception: ',  $e->getMessage(), "\n";
}
Untested.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

That is the try/catch blocks I was looking for...

But it didn't even occur to me that they wouldn't solve my problem...as my problem is a parse/compile time error, not runtime...so exceptions won't work :(

If there was a way to check a stack of errors/warnings the parser threw...I could maybe work with that :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

set_error_handler() is your friend.
Post Reply