I am working on a pretty large project and have read a lot about exception here at stackoverflow and on other websites. The result was, that there is 100% right or wrong. Some throw exceptions for unvalid user inputs, some don't. Some throw exceptions only for runtime errors, some don't...
I personally like the way to throw exceptions even for unvalid user inputs.
Now my problem is that I have for example a user that can comment on a statement of another user (for example his/her favorit music and so on). Every user is only allowed once to comment on it. Now the function which creates a database entry for the comment, checks whether the user has already commented on that statement. If yes, throw an exception. Normally I would say that I name this exception: ExceptionStatementAlreadyCommented but I have many other functions in this project and if I always create such specific exceptions I would end up with about 100 - 200 exceptions.
Does this affect the performance? I autoload the classes needed via the __autoload function, so the actual exception is only loaded when it is needed.
Is it a good way to name exceptions like this? In the past I had trouble with using one exception for different errors because when I catched the exceptions, I sometimes catch exception that I did not want to catch.
Thank you really much for your help!
Best regards,
Freddy
Throwing Exceptions in PHP Throwing a generic exception is almost as simple as it sounds. All it takes is to instantiate an exception object—with the first parameter of the Exception constructor being the error message—and then, "throw" it.
An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use.
Table of Contents ¶ PHP has an exception model similar to that of other programming languages. An exception can be throw n, and caught (" catch ed") within PHP. Code may be surrounded in a try block, to facilitate the catching of potential exceptions.
My feeling is that you are over using exceptions. As referenced by the name, an expection should only be thrown under Exceptional Circumstances.
Exceptions should not be used to control the flow of your application. Users entering bad data or not entering a correct username and password would be considered part of the normal flow of your application, not something exceptional.
You should only throw exceptions in exceptional circumstances that could potentially render the whole or part of the application unusable, for example, not being able to connect to the database.
This way, you will not end up with so many exceptions in the system that requires catching and perhaps rethrowing.
It is a good idea to have different exceptions, for example, I use the RedBean ORM to talk with my database, and it uses these Exceptions:
Being too specific about the exception can lead to a lot of confusion. For example, if I have an exception for missing sql column
, bad syntax
, missing value
, etc, it can become quite unmanagable. But if I have an SQL_Exception
, and use that exception for all of the above, then it is much more neater and simplier to manage.
As for performance, having to load a lot of classes (I am assuming they are in external files), can be taxing to the application. This can be partially aleviated by using APC, to cache interpreted PHP code in memory. But this is hard to tell without any profiling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With