When to catch the Exception vs When to throw the Exceptions?
I have been coding in Java for a while now. But sometimes, I don't
understand when I should throw the exception and when should I catch the
exception. I am working on a project in which there are lot of methods.
The hierarchy is something like this-
Method A will call Method B and Method B will call some Method C and
Method C will call Method D and Method E.
So currently what I am doing is- I am throwing exceptions in all the
methods and catching it in Method A and then logging as an error.
But I am not sure whether this will be the right way to do it? Or should I
start catching exceptions in all the Methods. So that is why this
confusion started in my- When should I catch the Exception vs When should
I throw the exceptions. I know it's a silly question but somehow I am
struggling to understand this major concept.
Can someone give me a detailed example of When to catch the Exception vs
When to throw the Exceptions so that my concepts gets cleared on this? And
in my case, should I keep on throwing the exception and then catch it in
the main calling Method A?
No comments:
Post a Comment