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

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

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

Postby mattpointblank » Thu Apr 30, 2009 3:26 am

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):

Syntax: [ Download ] [ Hide ]
 
<?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
mattpointblank
Forum Contributor
 
Posts: 304
Joined: Tue Dec 23, 2008 7:29 am

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

Postby Benjamin » Thu Apr 30, 2009 3:35 am

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:

Syntax: [ Download ] [ Hide ]
 
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.
Image
User avatar
Benjamin
Site Administrator
 
Posts: 6865
Joined: Sun May 19, 2002 10:24 pm

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

Postby allspiritseve » Thu Apr 30, 2009 11:04 am

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
User avatar
allspiritseve
DevNet Resident
 
Posts: 1174
Joined: Thu Mar 06, 2008 9:23 am
Location: Ann Arbor, MI (USA)

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

Postby Christopher » Thu Apr 30, 2009 1:34 pm

This may be of interest:

viewtopic.php?f=19&t=87653
(#10850)
User avatar
Christopher
Site Administrator
 
Posts: 12665
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Postby kaisellgren » Sat May 02, 2009 6:55 am

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.
User avatar
kaisellgren
DevNet Resident
 
Posts: 1675
Joined: Sat Jan 07, 2006 6:52 am
Location: Lahti, Finland.


Return to PHP - Theory and Design

Who is online

Users browsing this forum: No registered users and 3 guests