We all know it is needed.
But WHY is it needed in Java alone, when other similar languages that have exception handling capablities don’t require us to write “throws Exception”? Is there anyone who knows what was happening when Java language was designed and why they made it that way? Just curious.
P.S. This may not be a practical or really necessary question – it might not help me in anyway with my ongoing projects. But certain language features kindle my curiosity 😀
Edit
Looks like my question was very vague! I think I worded the question wrongly. We need to use the “throws Exception” kind of syntax at some points during programming when dealing with Java code. But something like that is never needed in C# or C++ or even VB.Net and PHP. So why Java alone insists on this?
As other answers here have pointed out, the
throwsclause is only required for checked exceptions, which is a feature that currently only exists in Java.The official answer as to why Java has checked exceptions is well documented:
However, this decision is highly controversial, even within the Java community:
Personally, I find checked exceptions to be useful only when your API makes a habit of catching all exceptions and re-throwing them as something appropriate to your abstraction layer. For example, an in-memory object cache that happens to use a disk or SQL backend to cache data should never throw
IOExceptionorSQLException— instead, it should throw (and declare) some user-defined exception likeCacheFailureExceptionor similar.Also, you might find Ned Batchelder’s article Exceptions in the Rainforest illuminating in regard to this question.