This is a very common question but i did not get satisfactory answer. To be precise, under which conditions should we declare the exceptions(using throws) and handle (try catch) in other conditions. Please suggests !
Asked
Active
Viewed 80 times
-2
-
3throw it when it should be thrown and catch it where you can really handle it – Philipp Sander Sep 24 '13 at 10:29
-
1I'm sure there are satisfactory answers all around the internet. Or all around this site alone... Please read the [FAQ] (And @PhilippSander has the definitive answer here.) – ppeterka Sep 24 '13 at 10:30
-
Philipp, We can throw any exception from a method, so why we need a try catch, or vice versa. we can handle any checked exception, then why we need to throw it. If you have any example, that would be great ! – JavaGeek Sep 24 '13 at 10:33
1 Answers
0
see what happens if you dont keep thorws with a method the other developer who is calling the method might not notice that this method will throw this exception now if he tests the method with a different set of input then there is a possibility that the other developer will not throw that exception but when the program is ready , the user might again fill the input to throw the exception so as a developer what you should do is , keep the throws keyword in method definition so the other developer knows that he has to handle or forward this sort of exception

Hussain Akhtar Wahid 'Ghouri'
- 6,527
- 8
- 50
- 85