php exception handling
am 11.10.2009 23:18:03 von Lars Nielsen
Hi,
I am trying to make an exception class that emails the errors to myself.
I have started by using the example by ask at nilpo dot com on
http://dk2.php.net/manual/en/language.exceptions.php.
It work ok but i want it NOT to show the errors on the php-page but only
show the details in the email and then just write eg "An error occurred"
or so on the webpage. Can anyone give me a hint about how to achieve
this?
Regards
Lars Nielsen
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: php exception handling
am 12.10.2009 01:09:46 von Tommy Pham
----- Original Message ----
> From: Lars Nielsen
> To: php-general@lists.php.net
> Sent: Sun, October 11, 2009 2:18:03 PM
> Subject: [PHP] php exception handling
>
> Hi,
>
> I am trying to make an exception class that emails the errors to myself.
> I have started by using the example by ask at nilpo dot com on
> http://dk2.php.net/manual/en/language.exceptions.php.
>
> It work ok but i want it NOT to show the errors on the php-page but only
> show the details in the email and then just write eg "An error occurred"
> or so on the webpage. Can anyone give me a hint about how to achieve
> this?
>
> Regards
> Lars Nielsen
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
Lars,
Here's a pseudo code:
try {
if (!$valid) {
throw new Exception("Test failed.");
} else {
// do something
}
} catch (Exception $e) {
// set/print a user friendly message to send to output
// set a detailed message using debug_backtrace() or any other information relevant for easier troubleshooting
// log and/or email detailed message
}
Regards,
Tommy
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: php exception handling
am 12.10.2009 01:15:21 von Larry Garfield
On Sunday 11 October 2009 6:09:46 pm Tommy Pham wrote:
> Lars,
>
> Here's a pseudo code:
>
> try {
> if (!$valid) {
> throw new Exception("Test failed.");
> } else {
> // do something
> }
> } catch (Exception $e) {
> // set/print a user friendly message to send to output
>
> // set a detailed message using debug_backtrace() or any other
> information relevant for easier troubleshooting // log and/or email
> detailed message
> }
>
> Regards,
> Tommy
Actually the else clause is not necessary. That's one of the nice things
about exceptions. If you throw an exception, processing jumps to the
appropriate catch and never returns.
try {
// Do normal stuff.
if (!$valid) {
throw new Exception('OMG!');
}
// Do more normal stuff.
}
catch (Exception $e) {
// Print user friendly message.
// Log detailed information or whatever you're going to do.
}
--
Larry Garfield
larry@garfieldtech.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: php exception handling
am 12.10.2009 01:44:50 von Al
Lars Nielsen wrote:
> Hi,
>
> I am trying to make an exception class that emails the errors to myself.
> I have started by using the example by ask at nilpo dot com on
> http://dk2.php.net/manual/en/language.exceptions.php.
>
> It work ok but i want it NOT to show the errors on the php-page but only
> show the details in the email and then just write eg "An error occurred"
> or so on the webpage. Can anyone give me a hint about how to achieve
> this?
>
> Regards
> Lars Nielsen
>
Don't sell exception handling short; it can be very useful for processing
control. Here is an example of a sequence of user input validations and checks.
All check functions throw an exception, with the details, if they find an error.
e.g., "Tag is invalid. Check spelling". This message is rendered so the
user can correct his/her mistake.
if(!empty($adminMiscSettingsArray['instrText']))
{
try{
checkValidTags($validHTMLtags,
allProxyTagsArray,adminMiscSettingsArray['instrText']);
checkTagMatching($emptyProxyTagsArray,$adminMiscSettingsArra y['instrText']);
checkTagNesting($emptyProxyTagsArray, $adminMiscSettingsArray['instrText']);
checkTextLinks($linksProxyTagsArray, $adminMiscSettingsArray['instrText']);
}
catch (Exception $e){
$instrCheckErrorMsg = $e->getMessage();
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: php exception handling
am 12.10.2009 09:45:43 von kranthi
cant http://us3.php.net/manual/en/function.set-exception-handler. php be used ?
function exception_handler($e) {
//mail('to', 'exception', $e->getMessage());
}
set_exception_handler('exception_handler');
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php