|
Posted by Gerard Samuel on 01/17/05 17:00
Jason Barnett wrote:
> Gerard Samuel wrote:
>
>> Im currently using php 5.0.3 on a dev box,
>> trying to figure out how to correctly use
>> exceptions in my code.
>
>
> Exceptions can be "A Good Thing".
>
>> Per chance, I was monitoring the __autoload() function,
>> and Im noticing that calls are being made to
>> exception classes that I've setup.
>> But nothing is being actually thrown.
>
>
> AFAIK you cannot throw an exception during the __autoload function.
> Or at least that was the way it was for me when I tried it back in
> 5.0.0b2. The answer I got was something along the lines of "the
> executor would have no idea where to return", but it made little sense
> to me and I probably am remembering that wrong anyways. :)
Yes, I read about this, and tried that eval() hack in the user comments
of the man page,
but there is a gotcha with that hack (that I wont explain here), that
caused me to stop using it. But I was only monitoring the __autoload()
function, to get a better understanding
of how it works.
>
>> As I understand it, __autoload(), only gets called, if a class
>> file hasn't been loaded for a class.
>
>
> Correct.
>
>> I know for a fact that all my exception classes are derived from
>> php's Exception class.
>
>
> This is definitely a good practice as there are some internal
> functions for those classes that are VERY useful to your own
> Exceptions... and you cannot get the same functionality without
> extending the base Exception class.
>
>> I've debugged it down to this ->
>> //try
>> //{
>> $db->connect();
>> //}
>> //catch(databaseException $e)
>> //{
>> // throw $e;
>> //}
>>
>> The ::connect() method is supposed to throw a databaseException if a
>> connection cannot be made.
>> In my tests, connections are made, and the exception is not thrown.
>> But the try/catch block above, for some reason, tries to throw
>> the databaseException (according to __autoload()).
>> If I comment it out the try/catch block, __autoload()
>> doesn't try to load the databaseException object.
>
>
> Um, perhaps you have things a little mixed up? __autoload is only
> used to get a definition for an unknown class; it doesn't actually do
> any object instantiation. It's used for "just in time" loading of a
> class definition if and only if the class / file hasn't already been
> included.
While I understand what you mean, what my chain of thought was at the time,
is why was an exception being loaded "just in time", and not being thrown.
And I think I know why it was doing what it was doing in my case.
I may be wrong here, but the line ->
catch(databaseException $e)
is what is *initiating* an exception object if it doesn't exist.
My understanding at the time, of try/catch blocks was that it only
enters the *catch*
stage, if an exception was thrown.
But it seems in reality, the *catch* stage is actually part of normal
code execution path.
This seems to make sense. If true, it would clarify my understanding of
the
try/catch block, and why __autoload() is being called on.
>
>>
>> I dont know if this is a bug in my code, as Im still new to exceptions.
>> Im just checking to see if anyone else experienced anything like
>> this before...
>>
>> Thanks
>
Navigation:
[Reply to this message]
|