Tuesday 15 September 2015

java - Why can a method be declared to throw many exceptions even if none of them are thrown? -


I was merging my source code with a colleague and I saw that he added an exception to the declaration Was a ritual; However, I knew that the exception would never really be thrown away by that method.

I wonder why the compiler did not warn me about "unannounced exception declared" (or something like that). I realize that you can declare any method of throwing an exception, even if it is thrown out by code in any of those exceptions.

Why is that so?

  throws the public zero Fu () IOException, introspection exception, best operation operation, ... {// nothing}  

  1. Overriding method can throw exceptions, even if it does not have a superclass.
  2. While retaining the rear compatibility, you can change the method later to throw one of the exceptions.

No comments:

Post a Comment