Catching php errors


is there any simple way to make php errors catchable like exceptions? How can i replace application error handler with one that will not display php error but will throw exceptions with it?

1 Like

In Yii 1.0.6 guide, see the section about 'Handling Errors Using an Action'

Thanks, but its not suitable for me. I need to catch php errors in my console app to make proper shutdown in case of error. Not to display error.



 set_error_handler(create_function('', "throw new Exception(); return true;"));

 //error generator


catch (Exception $e) { /*...*/}



Yes, but i thought that i can do the same with some Yii api.

Yii already catches PHP errors and throws exceptions (See CApplication::initSystemHandlers). However, not all PHP errors can be caught.

Yes, Yii catches PHP errors, but it does not convert them into exceptions but displays instead. I cant see any "throw $e" in CErrorHandler::handleError(). Sorry for my stubbornness but i cant get a thing.

To make it clear: i need to catch php errors like exceptions in such situation:




    fopen($nonExistentFile, 'r');


catch(Exception $e)



So im looking for api compatible way to make CErrorHandler::handleError throwing exceptions.

You are right that Yii handles PHP errors rather than throwing exceptions. I think you need to use the approach suggested by imasia.

Ок then, thanks for replies.


I think you need to use the approach suggested by imasia.

Its not handy to write this code each time i need it. I will think about extending CErrorHandler class instead.

I can write in my config something like that, can i?




Yes, that's better.

Thank you, guys. This topic helped me quite a bit!