Welcome Guest, Not a member yet? Register   Sign In
Handing errors in my code using Exceptions
#8

(09-21-2015, 04:21 PM)jLinux Wrote: If ErrorException or Exception shouldn't be used, I can create my own (http://php.net/manual/en/language.exceptions.php#91159)

Sorry for posting this separately from the response to includebeer above, but I missed this part before I posted that response.

You can certainly use those as much as you'd like, I just think it's more useful to use the available Exceptions for their specified purpose, especially if other people ever have to read your code (and, in my case, that other person is sometimes myself, 2 years later, wondering what I was thinking when I wrote that code). Who knows, you might even want to write an exception handler someday which handles specific types of exceptions in specific ways (though this requires slightly more effort in PHP than in some other languages, since PHP allows only one catch per try).

My primary point here is that the type of the Exception is usually more important than the message encapsulated in that Exception. If I catch an InvalidArgumentException, I know that I probably passed bad data to the function/method, so I probably don't want to continue using that data the same way (if the function has multiple arguments, I may not know which argument was invalid without some additional checks, but I know where to start if I want to check). I might even decide to only catch a subset of Exceptions and throw the rest so they can be handled by higher-level code or the framework's exception handler.
Reply


Messages In This Thread
RE: Handing errors in my code using Exceptions - by mwhitney - 09-22-2015, 07:59 AM



Theme © iAndrew 2016 - Forum software by © MyBB