Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    Another dumb question, maybe..

    Is it possible to manage errors from directory and file related functions
    in such a way that they can be displayed as part of the page markup and
    not splattered all over, or displayed instead of content?
    I am aware of the use of @ to suppress these types of errors, but they
    will also not be logged. So I write error messages that are returned in an
    organized manner and can be displayed. If I get them I am not necessarily
    able to discern the nature of the error. E.G. a file isn't written because
    a: the directory doesn't exist,
    b: the directory doesn't have write permission set correctly
    c: something was wrong on the read side and the string that the file
    contents are supposed to be in is empty ( the file size was not set
    because there was something wrong with the file reference on fread)
    etc, etc....
    I have been adding more and more tests for these details, but there has to
    be a limit somewhere.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yes. Use a custom error handler (see the api for set_error_handler) combined with a function for that purpose.
    You don't need to even do that with proper handling. You can check if a directory exists, if it is a directory, if it is writable prior to attempting to write to it. Same goes for reading, you can see if a file exists and if its readable prior to opening it.
    Using proper branching will eliminate the errors and will allow your own branch handling. You don't typically need to fetch the error since you know what caused it based on the is_ functions, but you can get it from the error_get_last() function or $php_errormsg in older versions.
    I would suggest there is no limit the the branches required; write one for any test you require to verify that the function will execute properly OR is stopped and captured. The only reason you feel that there is due to php being a language that guides developers into sloppy practices. Most other languages, particularly compiled ones, don't let you get away with trying to write to a directory that doesn't exist and isn't set to auto create; that would throw an exception or raise an error which would likely terminate the program.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    anotherJEK (01-30-2014)

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    branching...yes

    Yes I do write a lot of if/if-else/else to handle the possibility of errors, to the
    the point often that keeping the proper nesting becomes a challenge. I have
    done some c console programming, so I have my feet wet a bit with compiled
    languages. I have never used try and catch and have encountered advice against
    using it in php and javascript at least. I have also done some
    Flash Actionscript, that is compiled into byte code,
    like java, as I understand it.

    Thank you for the error_get_last() suggestion.
    What area of the php manual would that be in?
    The manual has expanded to the point that it is harder to
    find things I used find.
    (I realize I can go online to the php site and do a search for it).

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Whomever is advising you to not use try/catch obviously doesn't do object oriented programming. The concept of a try catch is so that exceptions can be handled when it is raised by a method. Since the logic of OO implies that some class is only interested in what that class can do, if it encounters a situation its not familiar with and doesn't have the capability of determining what to do with it, it simply throws an exception to allow the caller to determine what to do with it. In an OO world, I wouldn't catch or error control something and then print out a message. That would imply that the class knows how the output should be formed, and chances are it shouldn't be talking directly to the output.
    I put a feature request in with 5.1 for checked exceptions. I believe that this has been attempted but hasn't worked properly, so I'm still hoping its under consideration for 6.0.

    Error_get_last and set_error_handler can both be found in the documentation under Function Reference > Affecting PHP Behaviour > Error Handling
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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