Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Defining user-made Exceptions

Status
Not open for further replies.

kyru

Programmer
Jan 17, 2009
45
ES
I'm trying to build an user-made exception, I have the following code:

class CalcError extends Exception
{
function __construct($message)
{
parent::Exception($message);
}
}

try {
$i=2;
if ($i==1)
{
throw new Exception ( 'Unknown error', 501);
}
else
{
throw new CalcError("Message");
}
} catch (CalcError $c) {
print 'message: '.$c->getMessage();
}
catch (Exception $e) {
print 'message: '.$e->getMessage();
}

(The $i=2 was just made to check that a general exception is correctly thrown).

But for some reason it doesn't throw my defined exception. In fact, the script fails to continue executing,I've checked the code a lot and I don't understand what can fail.

Any idea? Thanks in advance.
 
What errors, notices and warnings do you get?

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
It gave the error:

Call to undefined method Exception::exception()

Anyway I finally solved it, it was something locical after all, it was just question of renaming __construct by exception.

Thanks for your interest.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top