exception issues while catching unnecessarily

gudii9
gudii9 used Ask the Experts™
on
exception java.net.MalformedURLException is never thrown in body of corresponding try statement
exception java.io.IOException is never thrown in body of corresponding try statement

i am getting above compilaion error in one of my facade manager class.

i wonder what that means

if i do not throw it but still catch that should be ok right right?
why i get compilation error

Please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
If an exception cannot be thrown by the try block, it cannot be caught by the catch block. A catch can only contain exceptions that can possibly be thrown. Just removing MalformedURLException and IOException from the catch will fix your error.

Author

Commented:
so i cannot give some exception anticipating that may come which is bit odd to me?

sometimes we do not know what to anticipate right?

Please advise

Commented:
The code dictates what exceptions can be thrown. If you're adding new code that could throw new exceptions, then you can add them to the catch. If you really want, you can just catch 'Exception'. It's not a great practice, but I think it achieves what you're looking to do.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial