PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Nov 20, 2014 9:11 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Thu Apr 30, 2009 3:26 am 
Offline
Forum Contributor

Joined: Tue Dec 23, 2008 7:29 am
Posts: 304
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


Top
 Profile  
 
PostPosted: Thu Apr 30, 2009 3:35 am 
Offline
Site Administrator
User avatar

Joined: Sun May 19, 2002 10:24 pm
Posts: 6874
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


Top
 Profile  
 
PostPosted: Thu Apr 30, 2009 11:04 am 
Offline
DevNet Resident
User avatar

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


Top
 Profile  
 
PostPosted: Thu Apr 30, 2009 1:34 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 12693
Location: New York, NY, US
This may be of interest:

viewtopic.php?f=19&t=87653

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sat May 02, 2009 6:55 am 
Offline
DevNet Resident
User avatar

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group