Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception Specification

Tags:

java

exception

Does Exception specification is a part of method signature? What I mean is:

public void someMethod(String myString) throws IOException

is 'throws IOException' a part of a signature of this method?

Thanks

like image 634
There is nothing we can do Avatar asked May 07 '10 09:05

There is nothing we can do


1 Answers

Following up on Jon Skeet's answer and in response to the comment

@ Jon Skeet Why then I cant have public void run() throws IOException in a class which implements Runnable? – Knowing me knowing you

Section 8.4.6 of the Java Language Specification (3rd ed) says:

A method that overrides or hides another method (Section 8.4.8), including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.

More precisely, suppose that B is a class or interface, and A is a superclass or superinterface of B, and a method declaration n in B overrides or hides a method declaration m in A. If n has a throws clause that mentions any checked exception types, then m must have a throws clause, and for every checked exception type listed in the throws clause of n, that same exception class or one of its supertypes must occur in the erasure of the throws clause of m; otherwise, a compile-time error occurs.

It's not a matter of method signature here, but a matter of not requiring callers to account for exceptions that aren't required to be checked by the 'original' method they are calling.

like image 164
Anonymoose Avatar answered Oct 19 '22 23:10

Anonymoose