Servus,
zum abfangen einer Exception möchte ich wissen, ob das Öffnen einer Datei mit FileInputStream erfolgreich war. Wie sieht der Rückgabewert im Fehlerfall aus?
Wer mehr wissen muss, hier die ganze Klasse.
Danke für eure Hilfe!!!
zum abfangen einer Exception möchte ich wissen, ob das Öffnen einer Datei mit FileInputStream erfolgreich war. Wie sieht der Rückgabewert im Fehlerfall aus?
Java:
FileInputStream fStream = new FileInputStream("datei1.txt");
int ret = fStream.read();
fStream.close();
[B][COLOR="Red"]if (fStream.equals(null)) { //<<<<<======== Dass frisst er nicht!!!![/COLOR][/B]
throw new MyIOException();
}
Wer mehr wissen muss, hier die ganze Klasse.
Java:
import java.io.*;
//import java.util.*;
public class ExceptionsDemo{
/* complete the class MyIOException. */
public class MyIOException extends Exception {
public MyIOException(String message) {
super(message);
}
public MyIOException() { super(); }
}
public static void main(String[] arg) {
ExceptionsDemo myDemo = new ExceptionsDemo();
myDemo.mainFunction();
}
private Object store;
private Float f;
public void mainFunction() {
/* The function readFile() could create an exception. Write a try ... catch block to explicitly catch the MyIOException exception,
and print out the exception message. */
try {
readFile();
}
catch (MyIOException e) {
System.out.println(e.getMessage());
}
}
public void readFile() throws MyIOException {//throws MyIOException {
/* The file datei1.txt doesn't exist. Write a try ... catch block in readFile() to catch the exception.
Within the exception handler, perform the following:
call printStackTrace() on the exception
create and throw a new MyIOException with the message Error in readFile() */
FileInputStream fStream = new FileInputStream("datei1.txt");
int ret = fStream.read();
fStream.close();
if (fStream.equals(null)) { //<<<<<======== Dass frisst er nicht!!!!
throw new MyIOException();
}
}
}
Danke für eure Hilfe!!!