What to call exceptions (PHP)?

I am working on a fairly large project and read a lot about the exception here on stackoverflow and other sites. The result was 100% right or wrong. Some rule out exceptions for unaccounted for user inputs, some do not. Some throw an exception only for runtime errors, some do not ...

I personally like the method of throwing exceptions, even for invalid user inputs.

Now my problem is that I have, for example, a user who can comment on another user's expression (for example, his / her favorite music, etc.). Each user is allowed to comment on it only once. Now the function that creates the database entry for the comment checks if the user commented on this statement. If so, throw an exception. Normally, I would say that I call this exception: ExceptionStatementAlreadyCommented but I have many other functions in this project, and if I always create such specific exceptions, I will have about 100 - 200 exceptions.

Does it affect performance? I automatically load the classes needed with the __autoload function, so the actual exception is only loaded when necessary.

Can the exceptions be called such? I used to have problems using one exception for different errors, because when I grabbed the exceptions, I sometimes found an exception that I did not want to catch.

Thank you very much for your help!

Respectfully,

Freddie

+4
source share
3 answers

It’s good to have an exception tree that extends from the most general to the most specific, for example, your StatementAlreadyCommentedException (the way it should be called, with the Exception part at the end) should inherit from the InvalidUserInputException or maybe an AlreadyExistsException exception, and they should inherit from what something more general like Exception .

That way you can catch all exceptions more specific than the one you are looking for (if you ever need to). (You can catch an Exception and it will catch all possible exceptions, you can catch an AlreadyExistsException , and it will catch any exception.

+3
source

I feel that you are using exceptions. As indicated by name, temptation should be chosen only in exceptional circumstances .

Exceptions should not be used to control the flow of your application. Users entering bad data or not entering the correct username and password will be considered part of the normal flow of your application, and not something exceptional.

You should only throw exceptions in exceptional circumstances that could potentially lead to the non-use of the whole or part of the application, for example, inability to connect to the database.

Thus, you will not get so many exceptions in the system that you need to catch and possibly rebuild.

It is a good idea to have different exceptions, for example, I use RedBean ORM to talk to my database and use these Exceptions:

  • RedBean_Exception_Security (when a security exception occurs).
  • RedBean_Exception_SQL (when the bad sql syntax occurred or some other problem).

Being too specific regarding exclusion 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 completely unmanageable. But if I have SQL_Exception and use this exception for all of the above, then it is much more simplified and easier to manage.

As far as performance is concerned, when loading a large number of classes (I assume that they are in external files), they can be taxed on the application. This can be partially alleviated by using APC to cache the interpreted PHP code in memory. But this is hard to say without any profiling.

+9
source

It makes no sense to create a separate exception class for each possible type of error. The idea is that the names of the exception classes should roughly describe the error, and each class must have the appropriate variables to set the exact error along with the exception message.

While the current approach will not affect your performance (at least there will be no exception classes), it will probably affect your maintainability, because it sounds like there are quite a few mental overheads, which can bring tangible benefits.

Things will also be much better if you create a well-designed exception hierarchy instead of creating 200 exception classes that directly extend \Exception .

For example, use something like a RecordAlreadyExistsException for all such cases, instead of ThisTypeOfRecordAlreadyExistsException for each case.

+2
source

All Articles