________________________________ From: Kyle Smith <kyle.smith@xxxxxxxxxxxxxx> To: Lamp Lists <lamp.lists@xxxxxxxxx> Cc: php-general@xxxxxxxxxxxxx Sent: Monday, April 13, 2009 9:52:36 AM Subject: Re: try - catch is not so clear to me... 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? let's take a look in example from php.net(http://us3.php.net/try) <?php function inverse($x) { if (!$x) { throw new Exception('Division by zero.'); } else return 1/$x; } try { echo inverse(5) . "\n"; echo inverse(0) . "\n"; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } // Continue execution echo 'Hello World'; ?> I would do the same thing, I think, less complicated: <?php function inverse($x) { if (!$x) { echo 'Division by zero'; } else return 1/$x; } echo inverse(5); echo inverse(0); // Continue execution echo 'Hello world'; ?> I know this is "too simple, maybe not the best example", but can somebody please explain "the purpose" of try/catch? Thanks. -LL Your example kind of defeats the point. The point of a try {} block is that it will attempt to execute code and execute catch on a true failure. Your function already is protected against failure. Consider this $x = 0; try { $y = 4 / $x; // This will divide by zero, not good. } catch (Exception $e) { echo "Error: $e" } More importantly, the try/catch should be in your function, not around the invocations of your function: function inverse($x) { try { return $x/0; } catch(Exception $e) { return false; } } Consider this also, simply echoing an error on divide by Zero might not be great if your function is called, say, before headers. Throwing exceptions can be re-caught by executing code, which can easily avoid pre-header output. Does that clear up the purpose a bit? I'm no expert, but that's my understanding. HTH, Kyle Yes and No... Right now I was thinking to start using try/catch block on places where could be problem in my code and, if there is an error - send to myself email with error info. E.g., I use right now something like this: function send_conf_email($to, $subject, $content) { $headers = "MIME-Versin: 1.0\n" . "Content-type: text/plain; charset=ISO-8859-1; format=flowed\n" . "Content-Transfer-Encoding: 8bit\n" . "Reply-To: <ll@xxxxxxxxx>\n". "From: LL\n" . "X-Mailer: PHP" . phpversion(); mail($to, $subject, $body, $headers); } function send_error_email_to_admin($email) { $to = 'LL@xxxxxxxxx'; $subject = '[Error Report] '.$email['subject']; $content = $email['content']; send_conf_email($to, $subject, $content); } if (!send_conf_email($to, $subject, $content)) { send_error_email_to_admin($subject, $content); } how would/should be the code by using try/catch() block? something like this: try { if (!send_plain_email($this->submitted_email, $subject, $body_plain)) { throw new Exception('Confirmation email is not sent'); } } catch (Exception $e) { send_error_email_to_admin($subject, $content . $e->getMessage()); } there is something "fishy" :-) -LL