Understanding Exceptions with try catch block

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

Post Reply
wingobaxter
Forum Newbie
Posts: 1
Joined: Wed Jul 25, 2007 12:57 pm

Understanding Exceptions with try catch block

Post 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]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

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

Post 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();
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

astions wrote:Nothing in that try block is throwing an Exception..
That's what I thought. :-D
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Re: Understanding Exceptions with try catch block

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