-1

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
Joseph hooper
  • 967
  • 4
  • 16
  • 35
  • What do you mean by "isn't running properly?" You really should use a modern Java IDE like Eclipse or IntelliJ. They will tell you exactly what is wrong with your code. – kuujo Mar 20 '15 at 04:40
  • You can catch exceptions of a higher abstraction not specific like you are, The Scanner throws a FileNotFoundException not a UnknownRegionException – Kenneth Clark Mar 20 '15 at 04:56
  • @KennethClark but since the UnknownRegionException extends the FileNotFoundException shouldn't it work anyway? – Joseph hooper Mar 20 '15 at 04:58
  • @KennethClark because it calls to the super? or is that not how it works – Joseph hooper Mar 20 '15 at 05:00
  • The problem with the FileNotFoundException is that Scanner throws it and not your exception. Scanner doesn't know about your exception, and you can't expect it to throw something it doesn't know about. If you were expecting that exception then it would be expected from some area of your code since you created it. Does that make sense? – kuujo Mar 20 '15 at 05:06
  • You can catch IOException because that is a superclass of FileNotFoundException, but you can't catch your exception because it's a subclass of FileNotFoundException and the Scanner only throws a FileNotFoundException – kuujo Mar 20 '15 at 05:07

1 Answers1

2

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.

kuujo
  • 7,785
  • 1
  • 26
  • 21
  • Yeah i get that i need to catch those exceptions but how do I utilize the UnknownRegionException (which extends file not found) if I have to catch with a filenotfound exception – Joseph hooper Mar 20 '15 at 05:03
  • Your exception is a subclass of FileNotFoundException that scanner doesn't know about and therefore cannot throw. The only way you could make it behave like an UnknownRegionException is thrown is by catching FileNotFoundException and then re throwing it as UnknownRegionException. – kuujo Mar 20 '15 at 05:11