SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Error handling

  1. #1
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error handling

    Hi,

    I was just about to write a large number of functions, one fo each possible error. I then thought that perhaps it maybe better to make one function called error and pass the error number to it. There is a control structure - that I can't remember the name of!!!! - but unlike if..then...else it takes a value and goes through the statments till it finds the one that matches, does anyone know what I'm talking about??? I know it's avaliable in VB/VBA but not sure about PHP???

    So what i want to know is, is it better to use loads of functions i.e. E0(), E1() ... E99() or to use one great big function?

    And does anyone know the function that I'm talking about, I've been trying to remember what it is for ages now. grrrrrr, smeggin memory.

    Thanks
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  2. #2
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    The control structure you are thinking of is switch(). If I was doing something like this I'd write one big function but then I'd probably lean towards OOP because this is a pretty big idea.

    Switch is very similar to VBs Select
    PHP Code:
    switch ( $foo ) {
       case 
    1:
          
    /* code */
          
    break;
       case 
    2:
          
    /* code */
          
    break;
       default:
          
    /* code */
          
    break;

    Code:
    Select Case intFrame
    		Case 1
    			imgMoles(intIndex).Picture = imgMoleHalf.Picture
    			showMole = False
    			Mole(intIndex).Hittable = False
    		Case 2
    			imgMoles(intIndex).Picture = imgMoleUp.Picture
    			showMole = False
    			Mole(intIndex).Hittable = True
    		Case 3
    			imgMoles(intIndex).Picture = imgMoleHalf.Picture
    			showMole = False
    			Mole(intIndex).Hittable = False
    		Case 4
    			imgMoles(intIndex).Picture = imgMoleMiss.Picture
    			showMole = False
    			Mole(intIndex).Hittable = False
    		Case 5
    			imgMoles(intIndex).Picture = imgMoleDown.Picture
    			showMole = True
    			Mole(intIndex).Hittable = False
    	End Select
    Hope that helped

  3. #3
    SitePoint Wizard Rick's Avatar
    Join Date
    Oct 2002
    Location
    Lancashire, UK
    Posts
    3,847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean a Switch?

    That Will loop through all the 'cases' until it finds a match, and will then display whatever message (or anything else) you have given for that case.

    Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements.
    PHP Code:
    // This...

    switch ($i) {
        case 
    0:
            print 
    "i equals 0";
            break;
        case 
    1:
            print 
    "i equals 1";
            break;
        case 
    2:
            print 
    "i equals 2";
            break;
    }

    // is equivalent to this...

    if ($i == 0) {
        print 
    "i equals 0";
    } elseif (
    $i == 1) {
        print 
    "i equals 1";
    } elseif (
    $i == 2) {
        print 
    "i equals 2";

    Oh - don't forget to put break; after each of your statements - otherwise PHP will continue looping through the list looking for matches.
    PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case.
    Anyway the PHP manual page is quite detailed so should explain it all.

    ZOO
    Rick

  4. #4
    SitePoint Wizard Rick's Avatar
    Join Date
    Oct 2002
    Location
    Lancashire, UK
    Posts
    3,847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DJ P@CkMaN
    The control structure you are thinking of is switch().
    Tuh - just typical, beaten to it by a minute



    ZOO ;-)
    Rick

  5. #5
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tuh - just typical, beaten to it by a minute
    lol

    Thanks, that was the function i was looking for!!!

    I'd probably lean towards OOP because this is a pretty big idea
    That was what i originally thought, but then thought that actualluy there probably wasn;t much point??? It's just a matter of echoing/printing a suitable error message, surely a function should be adequate?
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  6. #6
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Yes, a function would be plenty I'm just an OOP freak

  7. #7
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rofl. I've just started OOPHP
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  8. #8
    SitePoint Addict been's Avatar
    Join Date
    May 2002
    Location
    Gent, Belgium
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's just a matter of echoing/printing a suitable error message, surely a function should be adequate?
    Not always;
    Errors can have different levels:
    - You got errors that are critical, if such an error should occur, a system cannot function properly;
    Say for example, you store a website (configuration, content, the lots...) in a database system, and for some reason the database server is down... Simply echoing something like 'Database is down' surely would not suffice in a live website??
    - user errors: A user fills in invalid data in a form for example;
    The user should be clearly notified of his/her error, maybe the form should be re-displayed, etc...
    - "small" errors: these are probably the errors where a simple message display could be adequate;
    ex: you get some data from a db-table, but this is not critical data, if, for some reason, the data could not be fetched, you could echo a message saying 'Could not obtain data' or something...

    Also, how would you handle occurring errors ?
    Maybe an echo of a message is enough, maybe you want to log them to an application log, maybe you want the webmaster to be notified in an e-mail, ...

    What I'm getting at is this:
    Exception handling is part of a larger system, I don't think you can just write a simple function that handles every possible error and excpect it to work in whatever application you're writing.

    I'm still thinking on how to handle exceptions gracefully in my php apps, but I haven't quite figured it out yet. I'm thinking along these lines:
    Make a base class Exception, and make subclasses of it that represent the different type of exceptions that can occur. This should allow for
    1) a basic interface for Exception objects
    2) Extensibility: If in an app, you should need a new type of exception, simply write a sub class of Exception.
    basic methods of an exception should be methods like throw() and halt()...

    Anyway, purely theoretical and still a completely cluttered problem space in my mind ;-)
    Per
    Everything
    works on a PowerPoint slide

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Needham, MA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Outputting all of your errors to an external function does sound convenient, but just make sure that you include comments in your functions which explain what the error is before you pass it off. Imagine trying to go back and debug your function and only seeing calls to err96() and err45() instead of more explanatory things.

    So just think about what makes it an easier job for future coding and debugging, as well as what makes it easier to display errors.
    Tom Mollerus
    tmollerus@oneworkspace.com
    http://www.oneworkspace.com The affordable, simple, and secure way to manage your projects online

  10. #10
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The use of SWITCH - or anyother control structure wouldn't have been my first choice folks 8)

    A better and more easier way to script for and maintain would be to store your error messages in an array and address them that way, no ?

    PHP Code:
    array(
    'E1' => 'Oh **** - my database has packed up',
    'E2' => '...yet another database error',
    .
    .
    'E99' => 'and so on...'
    ); 
    And on the matter of varying levels of error state, this can be acheived using the above array as well 8)

    PHP Code:
    array('Fatal' => array(
    'E1' => 'First error...or whatever',
    'E2' ... etc
    ),
    'Warning' => array(
    'E1' => '....',
    'E2' => ... etc
    )
    ); 
    Hope this all helps you out ?

  11. #11
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe an echo of a message is enough, maybe you want to log them to an application log, maybe you want the webmaster to be notified in an e-mail, ...
    One thing at a time!!! lol. yeah that would of course all be preferred, but I'm making quite a big site at the mo, so am going to try and start with 'nice an simple' then beef it up latter.

    A better and more easier way to script for and maintain would be to store your error messages in an array and address them that way, no ?
    There's always someone who has to come up with an annoyingly good idea, lol.

    When i said 'one for each possible error' what i meant was one for all the common errors, and a couple of generic ones.
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  12. #12
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And the second Array option would cover this no ?

    You could have a lot of common errors under one 'state' and then have a few specific errors under their own specific 'state'...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •