Chris92 Posted September 11, 2011 Share Posted September 11, 2011 Is there any way to catch warnings without defining a warning handler? Example: try { $settings = parse_ini_file( 'main.ini' ); } catch( ErrorException $e ) { throw new ErrorException('No main configuration file found for webiste', '0003'); } Only ends up throwing a warning if the file doesn't exist. I want to catch it and throw an error. Quote Link to comment https://forums.phpfreaks.com/topic/246924-catch-warnings/ Share on other sites More sharing options...
xyph Posted September 11, 2011 Share Posted September 11, 2011 <?php try { // Suppress usual errors the function would echo using @ if( ($settings = @parse_ini_file( 'main.ini' )) === FALSE ) throw new ErrorException('No main configuration file found for webiste', '0003'); else { // everything's okay } } catch( ErrorException $e ) { echo 'Error found: '.$e->getMessage().' ('.$e->getCode().')'; } ?> Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/246924-catch-warnings/#findComment-1268098 Share on other sites More sharing options...
Chris92 Posted September 12, 2011 Author Share Posted September 12, 2011 Well then I might as well just right it out in an if rather than a try statement and not bother catch it. Can you only catch ErrorExceptions in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/246924-catch-warnings/#findComment-1268224 Share on other sites More sharing options...
xyph Posted September 12, 2011 Share Posted September 12, 2011 In that case, why not write a custom error handler that throws exceptions? Quote Link to comment https://forums.phpfreaks.com/topic/246924-catch-warnings/#findComment-1268235 Share on other sites More sharing options...
Adam Posted September 12, 2011 Share Posted September 12, 2011 A lot of languages have try..catch blocks, but the implementation can vary. With JavaScript for example, think of them more like PHP errors. You can't "throw" an exception in JS, but you can catch errors JS throws itself -- excluding syntax type errors of course. With PL/SQL (Oracle) there are set exceptions that you can catch within routines that are handed back to the application, for example "NO_DATA_FOUND" - ORA-01403, or you can define your own, but it's a very different definition/handling process than PHP. In your case there is no point in wrapping it within a try..catch block, unless you want to escalate the exception up to a parent catch block. If parse_ini_file threw exceptions on error, then it would also make sense to use one. Quote Link to comment https://forums.phpfreaks.com/topic/246924-catch-warnings/#findComment-1268258 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.