Exceptions

capri

Mitglied
Hallo Forum,

in meiner Klausur für Java habe ich folgende Aufgabe für Exceptions bekommen, die ich leider nicht lösen konnte. Bei der Klausureinsicht

Gegeben ist Code:
Fügen Sie an der Stelle 1 jeweils folgende Ausdrucke. Welche Aufruf(e) lassen sich kompilieren? Warum?

1. void eat(){}
2. void eat() throws Exception{}
3. void eat(int x) throws Exception{}
4. void eat() throws FileNotFoundException{}

Code:
import java.io.*;
class Food{
    void eat() throws IOException{    }
}
   
public class Pizza extends Food{
    public static void main(String[] args){
        new Pizza().eat();
    }
}
// Stelle 1
}

In der Klausureinsich wurde mir das so erklärt:

1. void eat(){}
Kompilierbar, weil keine Exceptions geworfen werden

2. void eat() throws Exception{}
Kompiliert nicht, weil Exception Oberklasse für IOException ist

3. void eat(int x) throws Exception{}
Kompiliert, weil die Signatur anders ist (int x)

4. void eat() throws FileNotFoundException{}
Kompilierbar, weil FileNotFoundException spezielle Exception von IOException ist

Ist es korrekt?
Ich habe das ganze in Eclipse ausprobiert und da lässt sich nur "void eat(){}" kompilieren.

Danke!
Capri
 
Stimmt. So korrekt:

Code:
import java.io.*;
class Food{
    void eat() throws IOException{    }
}
  
public class Pizza extends Food{
    public static void main(String[] args){
        new Pizza().eat();
    }
// Stelle 1
}

}
 
Code:
    // OK Ueberschreibt und keine Exception
    @Override
    void eat(){}

    //  ueberschreiben und weniger spezifischere Exception
    // Exception is not compatible with throws clause in Food.eat()
    @Override
    void eat() throws Exception{}

    // OK Ueberschreib nix
    void eat(int x) throws Exception{}

    // ok ubeschreibt und wird spezifischer
    @Override
    void eat() throws FileNotFoundException{}
 
Also wenn man mal annimmt dass die Stelle 1 eine geschweifte Klammer weiter vor soll hast du vollkommen recht.
Es lässt sich nur void eat(){} kompilieren. Weil nämlich für alle anderen Fälle der Aufruf von eat() ein paar Zeilen drüber nicht passt. Es fehlt nämlich die Angabe was mit der jeweiligen Exception geschehen sool.
 
Oh. Gar nicht gesehen. Der Teufel liegt im Detail.
Wenns nicht nur um die Vererbung geht kompiliert natürlich die main nicht, wegen Unhandled Exception oder so.
Ausser die main hat noch ein throws Expcetion dahinter.
 
Aha, das heißt, deine Erklärung #4 wäre richtig, wenn :
Code:
import java.io.*;
class Food{
    void eat() throws IOException{    }
}
public class Pizza extends Food{
    public static void main (String[] args) throws IOException {
        new Pizza().eat();
    }
// Stelle 1
}

}

?
 
Ja, so stimmt das was man dir in der Klausureinsicht gesagt hat. (Und für den Fall 4 fehlt noch ein import für FileNotFoundException, naja je nachdem wie man es schreibt. der import java.io.*; umfasst das natürlich auch)

Edit: Kannst du dir jetzt noch ein paar Punkte abholen ?? 🙂🙂
 
Zuletzt bearbeitet:

Neue Themen


Zurück
Oben