I'm trying to understand exceptions and I'm getting errors when I try to implement a custom one I made. I was given directions to make a no constructor that passes the string below to the super constructor. I keep getting compiler errors and I'm not sure how to fix them. The custom file looks like this
import java.io.*;
public class UnknownRegionException extends FileNotFoundException {
public UnknownRegionException() {
super("Could not find your region!");
}
}
and this block of code isn't running properly
public Adventure(String file) {
try {
File inFile = new File(file);
Scanner scan = new Scanner(inFile);
int i = 0;
while (scan.hasNext()) {
String name = scan.nextLine();
pokemon[i] = name;
i++;
}
} catch (UnknownRegionException e) {
System.err.println(e);
}
}
The errors i'm getting are below
D:\Documents\Google Drive\Homework\1331\HW8>javac Adventure.java
Adventure.java:23: error: unreported exception FileNotFoundException; must be ca
ught or declared to be thrown
Scanner scan = new Scanner(inFile);
^
Adventure.java:63: error: unreported exception PokemonAlreadyExistsException; mu
st be caught or declared to be thrown
throw new PokemonAlreadyExistsException(message);
^
Adventure.java:78: error: unreported exception PokemonAlreadyExistsException; mu
st be caught or declared to be thrown
throw new PokemonAlreadyExistsException();
^
Adventure.java:84: error: unreported exception PartyIsFullException; must be cau
ght or declared to be thrown
throw new PartyIsFullException();
^
Adventure.java:99: error: unreported exception FileNotFoundException; must be ca
ught or declared to be thrown
PrintWriter outWriter = new PrintWriter(outFile);
^
5 errors
Checked exceptions need to be either caught or declared in the method signature. You're catching UnknownRegionException
but the errors you're seeing indicate that the code may potentially throw several other exceptions such as FileNotFoundException
and PokemonAlreadyExistsException
and PartyIsFullException
. So, those either need to be caught in your catch
block or declared in the method signature like so:
public Adventure(String file) throws FileNotFoundException, PokemonAlreadyExistsException, PartyIsFullException {
It's common for checked exceptions to be seen in I/O related methods (such as operating on files). Unchecked exceptions which extend RuntimeException
are more common and don't have the necessary verbose syntax.
EDIT
Even though your code knows about your UnknownRegionException
, Scanner
does not know about it and therefore cannot throw it. Scanner only declares that it throws FileNotFoundException
. If you want it to behave as if it throws UnknownRegionException
you need to catch FileNotFoundException
and wrap the message in UnknownRegionException
public Adventure(String file) throws UnknownRegionException {
try {
File inFile = new File(file);
Scanner scan = new Scanner(inFile);
int i = 0;
while (scan.hasNext()) {
String name = scan.nextLine();
pokemon[i] = name;
i++;
}
} catch (FileNotFoundException e) {
throw new UnknownRegionException(e.getMessage());
}
}
Aside from that, there's no way for Scanner
to throw an exception that is a subclass of the exception it declares that it throws. Essentially, FileNotFoundException
knows about IOException
because it's a subclass of IOException
, but it doesn't know about UnknownRegionException
because UnknownRegionException
is a subclass of it. Your only option is to throw your exception yourself.