Comments & Discussion
Thanks for the course, but I missed the theory about the reasonable use of exceptions. Now it looks to me that at least 50% of almost any code can be surrounded by try/catch constructs and create dozens or even hundreds of exception classes. I understand that this is all very individual, but maybe you can tell a few words from personal experience how you determine when to use exceptions.
While it's individual, for me it always comes down to:
What will happen if there's an issue here? Will the flow break? Do I handle it correctly?
And once that is clear - you either add an exception or improve validation of the data. For example, imports are a great place to add exceptions, to prevent complete failure. Same goes for any public facing API - exceptions are a really nice way to handle situations where someone misused something (sent wrong input or just doesn't have the correct data inside). In any case, I wouldn't go too deep to handle everything, as some things will be handled by framework in a nice way (yet, it is important to have a bug tracker with it!). If it isn't handled - I'd see importance of this working and returning an output with a friendly message
Helloo thanks for this course. Having a question. Base on this above code example should I base all my database relative operation, like Eloquent retrieving and fetching (as you did for user) inside a try-catch ?
I no, when do your really recommand to use it ?
Hi , what if user type like this http://api.test.com/api/v1/users/3dd ? how to catch this error? I tried adding multiple exceptions but even Exception class does not catch it. can somebody help ?
Thank you for this course