I mean, in the definition. If I have a method of a class that implements an interface and I want to throw an exception, how can I do that if the interface don’t have a throws declaration.
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
How is the code relying on your class implementing the interface going to know that it has to handle the new exception? You have two options:
RuntimeException, which doesn’t need to be in thethrowsclause. However, any code calling this method does not have to catch this exception nor does it know that it can be thrown. So be careful when using this option. Document them where possible, but you’ll still face a problem when passing an object to an existing method such as a library or one that is built-in.