Forum Moderators: coopster

Message Too Old, No Replies

try, catch, exception in PHP?

         

pmdung

8:54 am on Jun 22, 2004 (gmt 0)

10+ Year Member



I know a little about C, C++, Java and I like their method to catch all of their exceptions. I tried to find this method in PHP but it is not found. Does anyone have a(n) approach/code to process exception because I don't like using a huge keyword 'if', 'return false' in my function.
Thanks

timster

1:13 pm on Jun 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebMasterWorld.

Sounds like you're looking for: eval

[us3.php.net...]

pmdung

2:10 pm on Jun 22, 2004 (gmt 0)

10+ Year Member



Thanks timster, I read it becareful but I found nothing which can me help!
I found this article [zend.com...] but I don't understand yet how it work and how to use it!

timster

2:35 pm on Jun 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you explain a little more about what you're trying to achieve? (Some of us haven't written C since college.)

pmdung

3:21 pm on Jun 22, 2004 (gmt 0)

10+ Year Member



Yes, now I have a program which has somme functions as follows:

Code in PHP for my requirement:


<?
/**
* This function execute a query then return the query result!
* Return SQL result
*/
function runquery($query){
return mysql_query($query);
}
/**
* This function check the input from user is valid?
* Return TRUE if valid, FALSE otherwise
*/
function dataCheck(){
if (!isset($_POST['Field1'])) return FALSE;
if (!isset($_POST['Field2'])) return FALSE;
if (!isset($_POST['Field3'])) return FALSE;
return TRUE;
}

/**
* This function call dataCheck(), if the input is valid, it call runquery() (many times)for save data to database
*/
function dataSave(){
if (!dataCheck()) return;
if (!runquery("INSERT INTO table1 SET FF=".$_POST['Field1'])) return;
if (!runquery("INSERT INTO table2 SET FF=".$_POST['Field2'])) return;
runquery("INSERT INTO table3 SET FF=".$_POST['Field3']);
}

/**
* Save data to database
*/
dataSave();
?>

Simulation code in C++ or Java:

sqlresult runquery(query q) throws DataException{
sqlresult r = mysql_query(q);
if (r == false)
throw new DataException('Query error');
return r;
}
int dataCheck() throws DataException{
if (!post[1])
throw new DataException('Field 1 is empty');
if (!post[2])
throw new DataException('Field 2 is empty');
if (!post[3])
throw new DataException('Field 3 is empty');
return 1;
}
void dataSave() throws DataException{
dataCheck();
//Operation '.' for string concatenate is only symbolic
runquery("INSERT INTO table1 SET FF=".post[1]);
runquery("INSERT INTO table1 SET FF=".post[2]);
runquery("INSERT INTO table1 SET FF=".post[3]);
}

try{
dataSave();
}catch(DataException e){
e->printBackTrace();
}

You can see that function dataSave() use many other functions with a simpler code

coopster

4:25 pm on Jun 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sounds to me like you'll want to brush up on PHP's Error Handling and Logging Functions [php.net].

pmdung

12:32 am on Jun 23, 2004 (gmt 0)

10+ Year Member



Thanks coopster,
I just want to have a method do these works each time when there is an error:
1. Generate a error message (OK, Error Handling and Logging Functions can solve this)
2. Exit current function, exit his parent functions,... reach to the function which have command catch to catch this error (I found nothing to do that :( )