- #1
Robben
- 166
- 2
Homework Statement
I want to redefine the FileNotFoundException by creating a new class which extends it, but I am having difficulty. For my class practice, if no file is selected or passed in, an UnknownFileException should occur.
Homework Equations
None
The Attempt at a Solution
My code is the following:
Java:
import java.io.FileNotFoundException;
public class UnknownFileException extends FileNotFoundException {
public UnknownFileException() {
super("We couldn't tell what file this is");
}
public UnknownFileException(String message) {
super(message);
}
}
Now, my other code which throws this exception:
Java:
public class practice {
public String[] array;
public String line;
public PrintWriter outputStream = null;
public Scanner inputStream = null;
public practice(String fileName) {
array = new String[100];
try {
inputStream = new Scanner(new FileReader(fileName));
line = inputStream.nextLine();
array[0] = line;
for (int i = 1; i < array.length; i++) {
array[i] = inputStream.nextLine();
}
inputStream.close();
outputStream = new PrintWriter(new FileOutputStream("newFile.txt"));
//outputStream.close();?
} catch(UnknownFileException e) {
System.out.println(e.getMessage());
}
}
But I get a compile error stating an unreported FileNotFoundException.