❔ ✅ What is the bestright thing to do when an Exception occurs say, in my MVC application?
Catch the exception and log
Catch the exception and do nothing.
Throw the exception.
Do nothing.
It depends on me as a dev
4 Replies
Catch the exception and do something to inform the user (if appropiate)
You could start with a generic message like: "Failed to [do something], please try again"
When is it appropriate to use
throw
then?At the place where the exception actually occurs
So lets say you have an api which has multiple layers; The controller which calls a
Handler
class, which itself calls a class that handles database stuff
If a user calls the controller to save something, but that fails in the database class, it will throw an exception
Which should be caught by either the handler that it was called for or the controller (other options are available too but I will exclude them for simplicity)
Then in that catch
clause of the controller you can return to the user that something failed.Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.
Was this issue resolved? If so, run /close
- otherwise I will mark this as stale and this post will be archived until there is new activity.