Hi, As I understand, the throws clause is used in the method declaration indicating that the method being created might throw an exception(which is being declared) and the application/program using this method should be able to handle it. Now, my questions are: 1. Since we always code try and catch for an exception we want to handle inside the method being created, why do we have to mention throws in the method/class declaration and let other methods/classes handle the exception? 2. If the above case is true, why do we need to have throws clause at all, if we are able to handle all the exceptions using try and catch? Am I making sense here? Please clarify my doubts. Thanks
Although you can catch an exception as soon as it's thrown, sometimes it's much more convenient to catch it in one of the outer calling functions. You have the option to defer handling to an outer function; if you do, you must declare the exception in a throws clause.
Java2learner Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however, your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy. Thanks again and we hope to see you around the ranch!!