Reputation: 1585
This is my code:
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(new File("story.txt"));
someFunction(s);
}
The story.txt
is in the the project root, but without the throws Exception
the code doesn't run.
Why?
Upvotes: 0
Views: 155
Reputation: 81159
If a method or constructor either throws an exception which isn't derived from RuntimeException
, or calls any method whose declaration indicates it must do so, then it must either catch that exception or declare that the exception may be thrown from it. This language feature is almost a good thing, but in its present form has been widely acknowledged as a mistake. Nonetheless, it is sufficiently firmly established as part of the Java language that it's not going anywhere.
Conceptually, it's good to make a distinction between exceptions which are likely to have special meaning to a method's immediate caller and those which would not. Unfortunately, the fact that an exception would have a special meaning to its immediate caller doesn't mean the immediate caller is going to be interested in it. The way Java implements checked exceptions, if Foo
is declared as throwing a checked exception and Bar
calls Foo
but isn't prepared to handle that exception, it must either declare itself as throwing that exception (even though it's unlikely to have any special meaning to its caller), or else catch the exception it has no hope of handling (perhaps rethrowing as a type derived from RuntimeException
). Catching and rethrowing as a type derived from RuntimeException
is semantically the best approach, but ends up being the most verbose, especially if one wants to avoid making RuntimeException
objects.
Note also that because there's no way a chained constructor call can be wrapped in a try
block, any constructor which is chained through a constructor that is declared as throwing any exceptions is required to declare itself as throwing those same exceptions. Things are implemented that way for a reason, but it unfortunately makes it difficult for a class constructor to distinguish exceptions which got thrown as part of the base-class construction process and those which got thrown in the construction of the derived class.
Upvotes: 1
Reputation: 1585
I asked my professor, and he explained that Java makes you handle exceptions.
You must be ready for them if a function might throw them.
You can either use:
try {...
} catch { ...
}
to handle exceptions yourself, or you can use:
throws Exceptions
to "pass them to the next level" (handle them later).
Thanks for the help!
Upvotes: 1
Reputation: 21961
Because, Scanner s = new Scanner(new File("story.txt"));
throws checked FileNotFoundException
. You should throws
or catch
checked exception for compilation.
Upvotes: 0
Reputation: 6394
You should throw a FileNotFoundException
, because the Scanner
constructor can throw one. Since it is a checked exception, you must either pass it further up the stack or handle it somehow.
Upvotes: 2
Reputation: 200158
You don't need throws Exception
, but it won't hurt any if you leave it like that.
What you do need is a throws FileNotFoundException
because the Scanner
constructor you are using is declared to throw that exception, and you don't catch any exceptions.
Upvotes: 6