Hallo,
kann mir bitte jemand bei folgendem Code helfen?
Ich verstehe absolut nur bahnhof und habe keine ahnung wie durchiteriert wird:
Die Ausgabe ist:
Greetings from g's finally
Greetings from f's finally
Got payload B 42
Wie kommt man darauf?
Besten Dank
kann mir bitte jemand bei folgendem Code helfen?
Ich verstehe absolut nur bahnhof und habe keine ahnung wie durchiteriert wird:
Java:
class PayloadA extends Throwable {
private double x;
PayloadA(double x) { this.x = x; }
public double get() { return x; }
}
class PayloadB extends Throwable {
private int x;
PayloadB(int x) { this.x = x; }
public int get() { return x; }
}
class ThrowAndCatch {
public static void main(String[] args) {
try {
f();
} catch (PayloadB p) {
System.out.println("Got payload B " + p.get());
} }
static void f() throws PayloadB {
try {
g();
} finally {
System.out.println("Greetings from f
s finally");
throw new PayloadB(42);
} }
static void g() throws PayloadA {
try {
throw new PayloadA(4711);
} finally {
System.out.println("Greetings from g
s finally");
} } }
Die Ausgabe ist:
Greetings from g's finally
Greetings from f's finally
Got payload B 42
Wie kommt man darauf?
Besten Dank
Zuletzt bearbeitet von einem Moderator: