Page 1 of 1

PHP's try/catch/throw - why use it (and how)?

Posted: Thu Apr 30, 2009 3:26 am
by mattpointblank
So in my quest to actually learn the language I've been messing around with for years, I'm investigating more OOP-style programming. This has led me to PHP's error throwing syntax.

I've heard bad things about it, like the suggestion that PHP's implementation of it is poor (example below):

Code: Select all

 
<?php 
try { 
    unlink("secret/secret.txt"); 
} catch(Exception $e) { 
    print "whoops!"; 
} 
?>
 
Apparently this code just outputs:

Warning: unlink(): SAFE MODE Restriction in effect. The script whose uid/gid is 501/501 is not allowed to access /home/xxx/secret owned by uid/gid 0/0 in /home/xxx/test.php on line 4

Eg, you'll never see the 'whoops!' message. Now, I'm trying to get my head around when to use this kind of structure, particularly if it's not as predictable as you'd hope. At the moment, my approach to programming interfaces where things can go wrong (eg, user submits a complex form including file uploads) is just like this:

- user submits form
- form fields which are required are checked for presence
- if any of the above are left empty, an error message is stored in a $_SESSION var and they're redirected to the form (to prevent clicking refresh and resubmitting etc) with their $_POST values intact
- same goes for any other errors, eg wrong filetype, upload error etc
- if all goes well, data is saved and redirect them to success page

What's wrong with this sort of approach? Do I 'need' to structure all of this in try/catch blocks to be considered a good programmer? Is there any advantage to doing it that way?

Thanks,
Matt

Re: PHP's try/catch/throw - why use it (and how)?

Posted: Thu Apr 30, 2009 3:35 am
by Benjamin
Typically, what I use try/catch blocks for are to accomplish a single event that can have multiple point of failures. Here's a vague example:

Code: Select all

 
try {
    # is variable foo an int?
    if (!preg_match('#^\d{1,10}$#', $foo)) {
        throw new Exception("foo must be a valid int");
    }
 
    # does foo exist?
    if (!isset($array[$foo])) {
        throw new Exception('foo does not exist');
    }
 
    # does this user have permission to modify foo?
    if (!user_has_permission_to_modify_foo()) {
        throw new Exception('you can not modify foo');
    }
 
    # were we able to modify foo?
    if (!modify_foo()) {
        throw new Exception('system error modifying foo');
    }
 
    # everything is ok
    $status->message('foo has been updated');
} catch (Exception $e) {
    $status->error($e->getMessage());
}
 
Hope that helps.

Re: PHP's try/catch/throw - why use it (and how)?

Posted: Thu Apr 30, 2009 11:04 am
by allspiritseve
mattpointblank wrote:I've heard bad things about it, like the suggestion that PHP's implementation of it is poor (example below):
A try/catch block is meant to catch exceptions, not errors. Since your code doesn't throw an exception, your message won't be displayed.

You can read more about exceptions here: http://us2.php.net/exceptions

Re: PHP's try/catch/throw - why use it (and how)?

Posted: Thu Apr 30, 2009 1:34 pm
by Christopher
This may be of interest:

viewtopic.php?f=19&t=87653

Re: PHP's try/catch/throw - why use it (and how)?

Posted: Sat May 02, 2009 6:55 am
by kaisellgren
I use try catch for situations where, for instance, a potential fatal error might occur and stop the execution. E.g., if you load a 32-bit .NET component in a 64-bit environment you easily end up in a fatal error and the execution just stops.