Sobes.tech
Junior — Middle

Is it possible to omit the mandatory throws declaration in a method declaration in Java?

sobes.tech AI

Answer from AI

In Java, the mandatory declaration of throws in a method cannot be omitted if the method can throw a checked exception. The compiler requires either handling the exception within the method (try-catch) or declaring it in the signature with throws. Exceptions of type RuntimeException (unchecked) do not need to be declared.

Example:

public void readFile() throws IOException {
    // code that may throw IOException
}