Re: try - catch is not so clear to me...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



________________________________
From: Bastien Koert <phpster@xxxxxxxxx>
To: Lamp Lists <lamp.lists@xxxxxxxxx>
Cc: Marc Steinert <lists@xxxxxxxxxx>; php-general@xxxxxxxxxxxxx
Sent: Tuesday, April 14, 2009 8:11:04 AM
Subject: Re:  try - catch is not so clear to me...




On Mon, Apr 13, 2009 at 11:34 PM, Lamp Lists <lamp.lists@xxxxxxxxx> wrote:




________________________________
From: Marc Steinert <lists@xxxxxxxxxx>
To: Lamp Lists <lamp.lists@xxxxxxxxx>
Cc: php-general@xxxxxxxxxxxxx
Sent: Monday, April 13, 2009 11:27:08 AM
Subject: Re:  try - catch is not so clear to me...


Basically try-catch gives you the ability to handle errors outside a class or method scope, by the calling instance.
This comes in handy, if you are programming in an object orientated way and thus enables you to seperate error handling from the rest of your functionality.
Means, your methods do only the things, they are meant to do, without bothering to handling occuring errors.
Hope, that made things clearer.

Greetings from Germany

Marc

Lamp Lists wrote:

>>  hi to all!
>>
>> actually, the statement in the Subject line is not 100% correct. I understand the purpose and how it works (at least I think I understand :-)) but to me it's so complicated way?
>>


-- http://bithub.net/
Synchronize and share your files over the web for free


My Twitter feed
http://twitter.com/MarcSteinert




Looks like I still didn't get it correctly:

try
{
   if (!send_confirmation_email($email, $subject, $content))

   {
       throw new Exception('Confirmation email is not sent');
   }

}
catch (Exception $e)
{

   send_email_with_error_to_admin($e, $content);
}

why am I getting both emails? I'm receiving confirmation email and email with error message - that I'm supposed to get if the first one is not sent for some reason?!?!?!?

thanks for any help.

-LL


     
what does this function [send_confirmation_email($email, $subject, $content)] return?

-- 

Bastien

Cat, the other other white meat





function send_confirmation_email($to, $subject, $body)
{    
  $headers =    "MIME-Versin: 1.0\n" .
                 "Content-type: text/plain; charset=ISO-8859-1; format=flowed\n" .
                 "Content-Transfer-Encoding: 8bit\n" .
                            "Reply-To: Contact <lamp.lists@xxxxxxxxx>\n".
                 "From: Contact <lamp.lists@xxxxxxxxx>\n" .
                 "X-Mailer: PHP" . phpversion();
        
    mail($to, $subject, $body, $headers) or die(mysql_errno());
}

$body is "regular" confirmation text: "Thank you for subscribing. To activate your account klick on the following link..." etc.




function send_email_with_error_to_admin($e, $content)
{
  $error_message  = "Caught exception: " . $e->getMessage() . "\n";
  $error_message .= "Code : " . $e->getCode()."\n";
  $error_message .= "File : " . $e->getFile()."\n";
  $error_message .= "Line : " . $e->getLine()."\n";
  
    $to_email = "lamp.lists@xxxxxxxxx";
    $subject = "[Confirmation Error Report] ".$e->getMessage();
    $content .= "\n--------------------------------------------------------------------------------------------------\n\n";
    $content .= "Error Report:\n\n".$error_message;
    $content .= "\n--------------------------------------------------------------------------------------------------\n\n";
    
    send_confirmation_email($to_email, $subject, $content);
}


      

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux