Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name exceptions (PHP)?

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

like image 618
Freddy Avatar asked Jul 13 '12 08:07

Freddy


People also ask

How can I create exception in PHP?

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.

What are exceptions in PHP?

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.

Does PHP exception?

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.


1 Answers

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:

  • RedBean_Exception_Security (when a security exception has occurred).
  • RedBean_Exception_SQL (when bad sql syntax or some other issue has occurred).

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.

like image 98
F21 Avatar answered Sep 26 '22 20:09

F21