当前位置:网站首页>When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?

When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?

2022-07-05 02:06:00 javail

problem :

I have been coding in Java for a while now. I've used Java Coding for some time .But sometimes, I don't understand when I should throw the exception and when should I catch the exception. But sometimes , I don't know when to throw an exception , When should exceptions be caught .I am working on a project in which there are lot of methods. I am working on a project with many methods .The hierarchy is something like this- The hierarchy is 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. So what I'm doing now is - I throw exceptions in all methods and in methods A Capture it in , Then record it as an error .

But I am not sure whether this will be the right way to do it? But I'm not sure if it's the right thing to do ?Or should I start catching exceptions in all the Methods. Or I should start catching exceptions in all methods .So that is why this confusion started in my- When should I catch the Exception vs When should I throw the exceptions. So that's why this mess started with my - When should I catch an exception and when should I throw an exception .I know it's a silly question but somehow I am struggling to understand this major concept. I know this is a stupid question , But somehow it is difficult for me to understand this main 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? Someone can give me a detailed example , explain When to catch the Exception vs When to throw the Exceptions So that my concept can be clarified ?And in my case, should I keep on throwing the exception and then catch it in the main calling Method A? As far as I'm concerned , I should keep throwing exceptions , Then call the method in the main A Capture it in ?


Solution :

Reference resources : https://stackoom.com/en/question/1GNHe
原网站

版权声明
本文为[javail]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140950131435.html