br0ken Posted June 26, 2008 Share Posted June 26, 2008 Hello All! I've written my own custom exception class to catch any errors I throw but I need to do a little more. I want to somehow catch ALL errors that are thrown. For example, errors that PHP throws such as syntax errors, or when a function cannot be found etc. I want to catch this error so that I can report it and then terminate the script. I've been reading up on set_error_handler() and have been trying to find a way using this to accomplish this task. Am I going in the right direction and does anyone have any advice? My aim has been to set my exception class as my error handler so that when any error occurs, it is recorded in the same way exceptions are that I have thrown. Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/111951-php-exceptionserrors-catch-all/ Share on other sites More sharing options...
DarkWater Posted June 26, 2008 Share Posted June 26, 2008 set_error_handler(create_function('$x, $y', 'throw new Exception($y, $x);'), E_ALL & ~E_NOTICE); Something like that. You can use your Exception handler's name if you want. Link to comment https://forums.phpfreaks.com/topic/111951-php-exceptionserrors-catch-all/#findComment-574595 Share on other sites More sharing options...
br0ken Posted June 26, 2008 Author Share Posted June 26, 2008 After searching on Google, this was the first thing I found but unfortunately this does not seem to work! Did you get this code of Google or have you used it and it worked before? Link to comment https://forums.phpfreaks.com/topic/111951-php-exceptionserrors-catch-all/#findComment-574599 Share on other sites More sharing options...
DarkWater Posted June 26, 2008 Share Posted June 26, 2008 No, it actually works. o-O Show me how you're trying to use it. Link to comment https://forums.phpfreaks.com/topic/111951-php-exceptionserrors-catch-all/#findComment-574600 Share on other sites More sharing options...
br0ken Posted June 26, 2008 Author Share Posted June 26, 2008 I'm not quite sure how to use it, that's the problem! Basically, my exception class is Error. When I throw an exception I do it like so: throw new Error("Invalid Email!); But then in the catch part I use $e->record() I've tried creating a function that encapsulates this process and adding that to the set_error_handler but no such luck! What code would you use to achieve this based on my exception class? Thanks! Link to comment https://forums.phpfreaks.com/topic/111951-php-exceptionserrors-catch-all/#findComment-574605 Share on other sites More sharing options...
DarkWater Posted June 26, 2008 Share Posted June 26, 2008 set_error_handler(create_function('$x, $y', 'throw new Error($y, $x);'), E_ALL & ~E_NOTICE); Link to comment https://forums.phpfreaks.com/topic/111951-php-exceptionserrors-catch-all/#findComment-574650 Share on other sites More sharing options...
br0ken Posted June 26, 2008 Author Share Posted June 26, 2008 No, this still isn't working for me. Could you explain the code you've given me please? For example, what do the $x and $y variables represent? When initialising my class I usually only require the one variable, Error($msg) so where have these come from? Also, what error would you reproduce to cause the new error handler to be ran? Link to comment https://forums.phpfreaks.com/topic/111951-php-exceptionserrors-catch-all/#findComment-574663 Share on other sites More sharing options...
br0ken Posted June 26, 2008 Author Share Posted June 26, 2008 Actually, you're right, it does work! Thank you for that! However, even though it worked and a few errors were reported that I've removed, sometimes I get the following where it just prints out a large text string like this: Fatal error: Uncaught exception 'Error' with message 'mysql_result() [<a href='function.mysql-result'>function.mysql-result</a>]: Unable to jump to row 0 on MySQL result index 39' in D:\inetpub\vhosts\presentationcompany.co.uk\httpdocs\ecomm\includes\class\error.php(6) : runtime-created function:1 Stack trace: #0 [internal function]: __lambda_func(2, 'mysql_result() ...', 'D:\inetpub\vhos...', 61, Array) #1 D:\inetpub\vhosts\presentationcompany.co.uk\httpdocs\ecomm\includes\functions\database.php(61): mysql_result(Resource id #39, 0, 0) #2 D:\inetpub\vhosts\presentationcompany.co.uk\httpdocs\ecomm\includes\functions\database.php(74): getvalueRS(Resource id #39, 0, 0) #3 D:\inetpub\vhosts\presentationcompany.co.uk\httpdocs\ecomm\includes\code\cart.php(263): querydbval('SELECT id FROM ...') #4 D:\inetpub\vhosts\tapcentre.com\httpdocs\secure\cart.php(4): cart_shipping_check('0') #5 {main} thrown in D:\inetpub\vhosts\presentationcompany.co.uk\httpdocs\ecomm\includes\class\error.php(6) : runtime-created function on line 1 Any idea what's causing this? Thanks, you've been a great help so far! Link to comment https://forums.phpfreaks.com/topic/111951-php-exceptionserrors-catch-all/#findComment-574667 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.