What is catch block? how to restrict the catch block to run?
anybody telll me
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Jaish MathewsRank #225 · 11K reputationPosted Oct 16, 2018, 7:26 PM EDT
Rafnas T PRank #280 · 8.8K reputationPosted Oct 17, 2018, 1:19 AM EDT
Manali Kadam35 reputationPosted Jan 8, 2019, 8:02 PM EST
Use of return statements are not restricted in a catch block.
Use of return is logical.
However, a simple principle to follow is, when I write a method with return type is “it should either return an expected value OR it must throw an exception”.
With this approach, you can always avoid the NullpointerExceptions. But, if you are catching an exception, and you don’t want it to bubble up - then a return can be written in the catch block or outside of it.
If you are returning something to notify an error, I recommend to use exceptions and not return values. Because exceptions are available for that purpose.