Jump to content

PHP Exceptions/Errors - Catch All


br0ken

Recommended Posts

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

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!

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?

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.