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

2009-04-15 Thread Al



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



Here is a practical example that may help you.

Each of the functions can throw an exception, which causes 
the flow to jump to the catch block.


try
{
$checksOK = true;
checkEmailAddr($userSubmitedDataArray[EMAIL_ADDR_FIELD]);
checkPhoneDigits($userSubmitedDataArray[PHONE_NUM_FIELD],'phone'); 


checkNotes($userSubmitedDataArray, $sizesArray);
}
catch (Exception $e)
{
//Message text in check functions
$userErrorMsg = $e-getMessage();
}

Here is one of the functions:

function checkEmailAddr($emailAddr)
{
if(empty($emailAddr))
{
throw new Exception(No email address provided);
}

if(!preg_match(%...@%, $emailAddr))
{
throw new Exception(Email address missing mailbox 
name.);

}

if(!filter_var($emailAddr, FILTER_VALIDATE_EMAIL))
{
throw new Exception(Email address error. Syntax is 
wrong. );

}

$domain = substr(strchr($emailAddr, '@'), 1);
if(!checkdnsrr($domain))
{
throw new Exception(Email address warning. 
Specified domain \$domain\ appears to be invalid. Check 
carefully.);

}
return true;
}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Try{} Catch()

2007-12-23 Thread Christophe Gosiau
Try{} catch{} makes more sense when your application is Object Oriented 
and you use the MVC model.
Your library performs some actions but when an error occures, it can't 
write directly to your browser.
Your modules speak with your library and checks if an error occures.. If 
so a decent error message can be shown...



Al schreef:

Try() and Catch() seems neat; but, I've not found it to be very practical.

Anyone using it? How?

Al...


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: try/catch type constructs?

2002-02-01 Thread CC Zona

In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] (Jon Drukman) wrote:

 the idea being that if any of the commands in the block fail, the database 
 package automatically does a die() and the block is terminated.
 
 the nearest i could get with php is:
 
 do {
mysql_query(UPDATE ...);
if (mysql_error()) { $error.=Couldn't update: . mysql_error(); break; }
 
mysql_query(SELECT ...);
if (mysql_error()) { $error.=Couldn't select: . mysql_error(); break; }
 
and so on
 } while (0==1);
 
 if ($error) { do error stuff }
 else { it worked }

Have you read http://www.php.net/manual/en/features.error-handling.php 
yet?  There are some ideas there which you might find useful.

FWIW, there is a die() function in PHP, so you can simplify the above to:

mysql_query(UPDATE ...) or die(Couldn't update: . mysql_error());
mysql_query(SELECT ...) or die(Couldn't update: . mysql_error());

-- 
CC

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]