R
R
Romi2021-10-09 10:56:33
PHP
Romi, 2021-10-09 10:56:33

When should custom Exceptions be made?

Having delved into the topic of exceptions :) I was puzzled by the following question:

- In what cases may it be necessary to create your own exceptions?

After all, there are already quite a few ready-made ones both in SPL and in frameworks?

What good reason would there be to throw your own exception?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Flying, 2021-10-09
@romicohen

Native exceptions are needed in cases where you want to handle (or, if you're a library author, provide handling capabilities for) some exceptional scenario in a special way. For example, look at how exceptions are organized in Doctrine.
Also, custom exceptions can be useful if you want to pass some additional information with this exception. For example, look at the exceptions for different HTTP status responses in Symfony.

C
catanfa, 2021-10-16
@catanfa

We make custom exceptions in all cases. Just for readability, conveying context, excluding debate during code reviews - whether to do a custom exception or not, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question