Page 1 of 2
Catching errors
Posted: Tue Nov 20, 2012 9:38 pm
by eazyGen
Hi Guys,
I am confused as to how the try / catch mechanism works in PHP. If I were to run this code:
Code: Select all
try {
$infinity = 100 / 0;
}
catch(Exception $e) {
alert("I have an error");
}
This crashes and the obvious error doesn't seem to get "caught".
Can anyone explain why please?
Many thanks,
S
Re: Catching errors
Posted: Tue Nov 20, 2012 10:10 pm
by Benjamin
Please see the manual page on how exceptions work in PHP:
http://us3.php.net/manual/en/language.exceptions.php
Re: Catching errors
Posted: Wed Nov 21, 2012 7:16 am
by social_experiment
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.
Code: Select all
<?php
try {
// code to try
}
catch (Exception $e) {
echo $e->getMessage();
}
?>
Re: Catching errors
Posted: Wed Nov 21, 2012 7:11 pm
by eazyGen
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.
Code: Select all
<?php
try {
// code to try
}
catch (Exception $e) {
echo $e->getMessage();
}
?>
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.
Any ideas?
Re: Catching errors
Posted: Wed Nov 21, 2012 7:29 pm
by Benjamin
You can't catch a ball that isn't thrown.
Re: Catching errors
Posted: Wed Nov 21, 2012 8:19 pm
by Eric!
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.
Re: Catching errors
Posted: Wed Nov 21, 2012 8:30 pm
by eazyGen
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?
Many thanks for your time and thoughts,
S
Re: Catching errors
Posted: Wed Nov 21, 2012 10:24 pm
by Benjamin
Post the code and we can show you how to catch the error.
Re: Catching errors
Posted: Wed Nov 21, 2012 10:37 pm
by eazyGen
Benjamin wrote:Post the code and we can show you how to catch the error.
Here you go:
set_exception_handler('exception_handler');
Code: Select all
try {
$con = new ezConnection($ipDatabaseType, $ipSchema, $ipHost, $ipPassword, $ipDBUser);
}
catch(Exception $e) {
$s9Output["s9ReturnCode"] = "ERROR";
$s9Output["s9ReturnData"] = $e->getMessage();
ob_get_clean(); // Trim Output Buffer
echo ezJSON::jsonEncodeData("s9Output", $s9Output);
return;
}
Many thanks for your time,
S
Re: Catching errors
Posted: Wed Nov 21, 2012 11:08 pm
by Benjamin
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?
Code: Select all
try {
if (false == $con = new ezConnection($ipDatabaseType, $ipSchema, $ipHost, $ipPassword, $ipDBUser)) {
throw new Exception('Database connection failed');
}
} catch(Exception $e) {
$s9Output["s9ReturnCode"] = "ERROR";
$s9Output["s9ReturnData"] = $e->getMessage();
ob_get_clean(); // Trim Output Buffer
echo ezJSON::jsonEncodeData("s9Output", $s9Output);
return;
}
Or something like:
Code: Select all
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.
Re: Catching errors
Posted: Wed Nov 21, 2012 11:20 pm
by eazyGen
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?
Code: Select all
try {
if (false == $con = new ezConnection($ipDatabaseType, $ipSchema, $ipHost, $ipPassword, $ipDBUser)) {
throw new Exception('Database connection failed');
}
} catch(Exception $e) {
$s9Output["s9ReturnCode"] = "ERROR";
$s9Output["s9ReturnData"] = $e->getMessage();
ob_get_clean(); // Trim Output Buffer
echo ezJSON::jsonEncodeData("s9Output", $s9Output);
return;
}
Or something like:
Code: Select all
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.
Does that sound about tight to you?
Manhy thanks,
S
Re: Catching errors
Posted: Wed Nov 21, 2012 11:44 pm
by Benjamin
Define "Crashes". What exactly happens? Do you have a specific error message?
Re: Catching errors
Posted: Thu Nov 22, 2012 12:10 am
by eazyGen
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.
S
Re: Catching errors
Posted: Thu Nov 22, 2012 12:19 am
by Benjamin
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.
Re: Catching errors
Posted: Thu Nov 22, 2012 12:31 am
by eazyGen
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.
Could you expand a bit on an auto loader please?
Many thanks,
S