Java exception handling concept. why do compiler allow to write exceptions in throws section even if it can not be thrown? -


i investigate java exception handling , faced following behaviour. non me.

consider code snippet:

snippet 1

public void g(){         try {          } catch (filenotfoundexception e) {//any checked exception          }  } 

it compile error message

unreachable catch block filenotfoundexception. exception never thrown try statement body 

snippet2

public void g() throws filenotfoundexception{ } 

it compiles fine.

therefore, on results of first code snippet, compiler can calculate if method possible or impossible throws exception.

and can make conclusion made especially. don't why.

point of question - why compiler allow write exceptions in throws section if can not thrown?

i want understand full concept.

p.s. understand behavior corresponds documentation question understanding of exception handling concept in java.

the compiler allows because throws clause of method part of signature of method, rather part of implementation. possible implementation might change @ point, while keeping signature same. old implementation might have thrown checked exception, new 1 might not. or designer of signature might have wanted give implementer flexibility throw checked exception when not necessary.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -