Catching errors

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!

Moderator: General Moderators

User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Catching errors

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Catching errors

Post by Benjamin »

Please see the manual page on how exceptions work in PHP: http://us3.php.net/manual/en/language.exceptions.php
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Catching errors

Post 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();
}
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: Catching errors

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Catching errors

Post by Benjamin »

You can't catch a ball that isn't thrown.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Catching errors

Post 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.
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: Catching errors

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Catching errors

Post by Benjamin »

Post the code and we can show you how to catch the error.
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: Catching errors

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Catching errors

Post 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.
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: Catching errors

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Catching errors

Post by Benjamin »

Define "Crashes". What exactly happens? Do you have a specific error message?
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: Catching errors

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Catching errors

Post 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.
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: Catching errors

Post 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
Post Reply