I'm just trying to make my understanding of Exception mechanism more clear.
If something goes wrong when an app is executed, the runtime automatically throws an appropriate exception. If it is so, then why I should sometimes make some check and explicitly throw a specific exception?
How to identify such cases (when an exception should be explicitly thrown in code)?
I don't put any example here on purpose, because I'd like to get a general understanding of the throwing exceptions approach.
I think the general approach should be Fail Fast - you want to identify and handle invalid program conditions as soon as possible, this would mean not just when a particular input is needed within your program logic but as soon as you "receive" this input (which could be way earlier in time and/or lines of codes).
For i.e. public methods that means validating an input first, and if appropriate throw an exception if the input violates what your method expects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With