Non ho provato questo, quindi non so se la JVM limiterebbe qualcosa del genere, ma forse potresti compilare il codice che genera ChuckNorrisException, ma in fase di esecuzione fornire una definizione di classe di ChuckNorrisExceptioncui non si estende Throwable .
AGGIORNARE:
Non funziona Genera un errore verificatore:
Exception in thread "main" java.lang.VerifyError: (class: TestThrow, method: ma\
in signature: ([Ljava/lang/String;)V) Can only throw Throwable objects
Could not find the main class: TestThrow. Program will exit.
AGGIORNAMENTO 2:
In realtà, puoi farlo funzionare se disabiliti il verificatore del codice byte! ( -Xverify:none)
AGGIORNAMENTO 3:
Per quelli che seguono da casa, ecco lo script completo:
Creare le seguenti classi:
public class ChuckNorrisException
extends RuntimeException // <- Comment out this line on second compilation
{
public ChuckNorrisException() { }
}
public class TestVillain {
public static void main(String[] args) {
try {
throw new ChuckNorrisException();
}
catch(Throwable t) {
System.out.println("Gotcha!");
}
finally {
System.out.println("The end.");
}
}
}
Compilare le classi:
javac -cp . TestVillain.java ChuckNorrisException.java
Correre:
java -cp . TestVillain
Gotcha!
The end.
Commenta "estende RuntimeException" e ricompila ChuckNorrisException.javasolo :
javac -cp . ChuckNorrisException.java
Correre:
java -cp . TestVillain
Exception in thread "main" java.lang.VerifyError: (class: TestVillain, method: main signature: ([Ljava/lang/String;)V) Can only throw Throwable objects
Could not find the main class: TestVillain. Program will exit.
Esegui senza verifica:
java -Xverify:none -cp . TestVillain
The end.
Exception in thread "main"