PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
the code itself isn't a problem but there isn't an alert() function in php. If you've read the url supplied by Benjamin you will likely see that inside the catch section you often echo the message that you throw for the particular exception.
social_experiment wrote:the code itself isn't a problem but there isn't an alert() function in php. If you've read the url supplied by Benjamin you will likely see that inside the catch section you often echo the message that you throw for the particular exception.
Yes, you are quite right - I added the alert() in error.
However, I am still having problems because I have a very important function that must either work or end with dignity and a meaningful message. There is no value I can test and then throw an exception. I just want the catch to catch any errors that occur in the code. All the errors I can trap I already do so and manage them.
So there is nothing I can throw and I was hoping that the try / catch mechanism would kick in when a catastrophic takes place.
To give an example, I have one line of code that tests the connection to a database based on some parameters. If the parameters are wrong in some way, the function connection statement crashes without getting caught and I don't believe there is any excfeption I can throw with it.
eazyGen wrote:I have one line of code that tests the connection to a database based on some parameters. If the parameters are wrong in some way....
Can't you test for those conditions? Like Benjamin points out if the function doesn't throw an exception and crashes there's nothing to catch, so you have to test the parameters before calling it.
Benjamin wrote:You can't catch a ball that isn't thrown.
Quite - I appreciate that.
So does that mean that my piece of code that checks a database connection will either work or fail with an error that I cannot manage in any way? Or is there a way (as in Java) to create an exception handler (or anything else for that matter) to manage the error?
Ok great. So now we need to know what the return values of ezConnection are when it succeeds vs when it fails. Does it return false on failure? Does it set an error variable?
Benjamin wrote:Ok great. So now we need to know what the return values of ezConnection are when it succeeds vs when it fails. Does it return false on failure? Does it set an error variable?
try {
$con = new ezConnection($ipDatabaseType, $ipSchema, $ipHost, $ipPassword, $ipDBUser);
if ($con->hasError()) {
throw new Exception($con->getError());
}
} catch(Exception $e) {
$s9Output["s9ReturnCode"] = "ERROR";
$s9Output["s9ReturnData"] = $e->getMessage();
ob_get_clean(); // Trim Output Buffer
echo ezJSON::jsonEncodeData("s9Output", $s9Output);
return;
}
The specific code will depend on how the database class handles errors.
This is extremely helpful and I am very grateful.
Please bear with me as a colleague wrote the routine I am calling and it is failing. Based on what you have taught me, he needs to start to handle errors within that function, becuase it crashes before I can test for false or anything else.
Benjamin wrote:Define "Crashes". What exactly happens? Do you have a specific error message?
Yes, there is a file not found
Error (2) - include(C:\xampp\htdocs\eazy-gen\alpha2/util/database/dbtype/ezDBType2.php) [function.include]: failed to open stream: No such file or directory
I know this sounds daft not to have a file found, but it makes a little sense in the context of the app. There should be a file per database type. and this one would seem not to be there.
Ideally the system would use an auto loader then throw an error when a file is not found. In this case the class would need to handle that. You could likely modify it if you wanted. The most likely case you need to ensure is handled is what happens if the database engine is not responding or the connection is lost at some point during code execution.
Benjamin wrote:Ideally the system would use an auto loader then throw an error when a file is not found. In this case the class would need to handle that. You could likely modify it if you wanted. The most likely case you need to ensure is handled is what happens if the database engine is not responding or the connection is lost at some point during code execution.