spidey
22-05-2008, 14:49
Salve ragazzi, non mi è chiara una cosa nel seguente programma, spero che qualcuno possa essermi di aiuto:
public class Rethrowing {
public static void f() throws Exception {
System.out.println("originating the exception in f()");
throw new Exception("thrown from f()");
}
public static void g() throws Throwable {
try {
f();
} catch(Exception e) {
System.err.println("Inside g(),e.printStackTrace()");
e.printStackTrace();
throw e.fillInStackTrace();
}
}
public static void main(String[] args) throws Throwable {
try {
g();
} catch(Exception e) {
System.err.println(
"Caught in main, e.printStackTrace()");
e.printStackTrace();
}
}
}
L'output è il seguente:
originating the exception in f()
Inside g(),e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:10)
at Rethrowing.g(Rethrowing.java:14)
at Rethrowing.main(Rethrowing.java:25)
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.g(Rethrowing.java:19)
at Rethrowing.main(Rethrowing.java:25)
Da esso si evince che nel main viene intercettata una eccezione di tipo Exception, mi domando come questo sia possibile, in quanto all'interno di g(), tramite la chiamata e.fillInStackTrace(), viene generato e lanciato un oggetto eccezione di tipo "Throwable" e non Exception. Inoltre non mettendo un catch di Throwable all'interno del main siamo costretti a inserire la specifica dell'eccezione throws Throwable accanto alla dichiarazione del main e questo dimostra il fatto che g() in effetti lancia un Throwable. Mi chiedo dunque come sia possibile che all'interno del main venga intercettato un oggetto eccezione di tipo Exception?
Spero di aver esposto il mio problema in maniera chiara
Vi ringrazio in anticipo ;)
public class Rethrowing {
public static void f() throws Exception {
System.out.println("originating the exception in f()");
throw new Exception("thrown from f()");
}
public static void g() throws Throwable {
try {
f();
} catch(Exception e) {
System.err.println("Inside g(),e.printStackTrace()");
e.printStackTrace();
throw e.fillInStackTrace();
}
}
public static void main(String[] args) throws Throwable {
try {
g();
} catch(Exception e) {
System.err.println(
"Caught in main, e.printStackTrace()");
e.printStackTrace();
}
}
}
L'output è il seguente:
originating the exception in f()
Inside g(),e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:10)
at Rethrowing.g(Rethrowing.java:14)
at Rethrowing.main(Rethrowing.java:25)
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.g(Rethrowing.java:19)
at Rethrowing.main(Rethrowing.java:25)
Da esso si evince che nel main viene intercettata una eccezione di tipo Exception, mi domando come questo sia possibile, in quanto all'interno di g(), tramite la chiamata e.fillInStackTrace(), viene generato e lanciato un oggetto eccezione di tipo "Throwable" e non Exception. Inoltre non mettendo un catch di Throwable all'interno del main siamo costretti a inserire la specifica dell'eccezione throws Throwable accanto alla dichiarazione del main e questo dimostra il fatto che g() in effetti lancia un Throwable. Mi chiedo dunque come sia possibile che all'interno del main venga intercettato un oggetto eccezione di tipo Exception?
Spero di aver esposto il mio problema in maniera chiara
Vi ringrazio in anticipo ;)