Page 1 of 1

Understanding Exceptions with try catch block

Posted: Wed Jul 25, 2007 1:06 pm
by wingobaxter
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi All,

Fairly new to PHP and I have a question on catching exceptions.
In the following code:

Code: Select all

function getAddressList() {
    try {
        $string = 'a string'; 
        [b]explode($string); [/b]
        return $addrDao->getAddressList();
    }
    catch(Exception $e) {
        return $e;	
    }
}
This code will catch the fact that the explode method is wrong with a message
'Wrong parameter count for explode()'.

However, if I change the call to explod($string) (which does not exist at all) the error is never
caught in the catch block. Also, if I reference a null object within the try block it is not caught in the catch block.

Does the try/catch of PHP catch only a certain type of exceptions.

Thanks for any info.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jul 25, 2007 1:20 pm
by superdezign
I could be incorrect, but I believe that triggering errors and throwing exceptions are not the same. try...catch blocks catch exceptions (which is why you specify the Exception type in the catch statement). If you aren't getting an error at all, then you likely need to turn on error_reporting.

Does that code really return an exception message? Or does it trigger an error? I don't believe that explode() throws an exception.

Posted: Wed Jul 25, 2007 3:34 pm
by Benjamin
Nothing in that try block is throwing an Exception..

Code: Select all

try {
    if (false)
    {
        throw new Exception("something is false ");
    }
} catch (Exception $e) {
    echo $e->getMessage();
}

Posted: Wed Jul 25, 2007 3:59 pm
by superdezign
astions wrote:Nothing in that try block is throwing an Exception..
That's what I thought. :-D

Re: Understanding Exceptions with try catch block

Posted: Wed Jul 25, 2007 5:09 pm
by stereofrog
wingobaxter wrote: Does the try/catch of PHP catch only a certain type of exceptions.
There are three types of exceptional "signals" in php: legacy errors (warnings and notices), true exceptions and fatals.
"try/catch" can only trap true exceptions directly, it's also possible to convert errors to exceptions using custom error handler. However, when php encounters a fatal (e.g. undefined function, out of memory etc), it exits immediately, it's not possible to trap or handle this type of signal.

Examples:

Code: Select all

explode('foo'); // legacy warning
echo $no_such_var; // legacy notice
spl_autoload_register('blah'); // exception
no_such_func(); // fatal