Reputation: 2796
I'm pretty sure this is an easy one but I could not find a straight forward answer. How do I call a method with a throws FileNotFoundException
?
Here's my method:
private static void fallingBlocks() throws FileNotFoundException
Upvotes: 0
Views: 30588
Reputation: 597114
You simply catch
the Exception or rethrow it. Read about exceptions.
Upvotes: 3
Reputation: 1534
You call it like any other method too. However the method might fail. In this case the method throws the exception. This exception should be caught with a try-catch statement as it interrupts your program flow.
Upvotes: 2
Reputation: 59660
Isn't it like calling a normal method. The only difference is you have to handle the exception either by surrounding it in try..catch or by throwing the same exception from the caller method.
try {
// --- some logic
fallingBlocks();
// --- some other logic
} catch (FileNotFoundException e) {
// --- exception handling
}
or
public void myMethod() throws FileNotFoundException {
// --- some logic
fallingBlocks();
// --- some other logic
}
Upvotes: 2
Reputation: 500407
You just call it as you would call any other method, and make sure that you either
FileNotFoundException
in the calling method;FileNotFoundException
or a superclass thereof on its throws
list.Upvotes: 3
Reputation: 12538
Not sure if I get your question, just call the method:
try {
fallingBlocks();
} catch (FileNotFoundException e) {
/* handle */
}
Upvotes: 2
Reputation: 1500805
You call it, and either declare that your method throws it too, or catch it:
public void foo() throws FileNotFoundException // Or e.g. throws IOException
{
// Do stuff
fallingBlocks();
}
Or:
public void foo()
{
// Do stuff
try
{
fallingBlocks();
}
catch (FileNotFoundException e)
{
// Handle the exception
}
}
See section 11.2 of the Java Language Specification or the Java Tutorial on Exceptions for more details.
Upvotes: 8