what is catch block? how to restrict the catch block to run?
anybody telll me
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaish MathewsPosted Oct 16, 2018, 11:26 PM
Manali KadamPosted Jan 9, 2019, 1:02 AM
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.
Rafnas T PPosted Oct 17, 2018, 5:19 AM