Skip to content
Advertisement

Java checked exceptions

I’m trying to understand checked exceptions in Java and have the following query.

Is the following correct: If a method has the potential to throw a checked exception, of any type, that exception must either be

  1. declared using the throws keyword, or
  2. caught by the respective method.

If the above is correct, does this mean that I need to understand every single checked exception that is built in to Java so that I know whether my method will have the potential to throw that exception? Or should I just attempt to compile my code and then amend my code based on the compile-time errors?

Advertisement

Answer

If the above is correct, does this mean that I need to understand every single checked exception that is built in to Java […]?

Yes, your statements are correct, but no one expects you to know of all potential checked exceptions when programming. And you don’t have to if you have a compiler, or better, an IDE at hand.

Either you go back and forth between compiler output and code as you suggest in your question, or you install an IDE such as Eclipse which gives you direct notifications if you violate the rule:

enter image description here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement