Why can a method be declared for many exceptions, even if none of them are selected?

I combined my source code with my colleague and I saw that he added an exception that would be thrown into the method declaration; however, I knew that an exception would never really be thrown from this method.

I wonder why the compiler did not warn me about the "declared non-excluded exception" (or something like that). I understand that you can declare a method that throws N exceptions, even if none of these exceptions are thrown by code in the method.

Why is this?

public void foo() throws IOException, IntrospectionException, BadStringOperationException, ... { //do nothing } 
+8
java exception
source share
1 answer
  • Subclasses that override a method can throw an exception, even if its superclass does not work.
  • You can later change the method to throw one of these exceptions while maintaining backward compatibility.
+13
source share

All Articles