No lo he intentado, así que no sé si la JVM restringiría algo como esto, pero tal vez podría compilar código que arroje ChuckNorrisException, pero en tiempo de ejecución proporcionar una definición de clase ChuckNorrisExceptionque no extienda Throwable .
ACTUALIZAR:
No funciona Genera un error de verificador:
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.
ACTUALIZACIÓN 2:
En realidad, puede hacer que esto funcione si deshabilita el verificador de código de bytes. ( -Xverify:none)
ACTUALIZACIÓN 3:
Para aquellos que siguen desde casa, aquí está el script completo:
Crea las siguientes clases:
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.");
}
}
}
Compilar clases:
javac -cp . TestVillain.java ChuckNorrisException.java
Correr:
java -cp . TestVillain
Gotcha!
The end.
Comente "extiende RuntimeException" y recompile ChuckNorrisException.javasolo :
javac -cp . ChuckNorrisException.java
Correr:
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.
Ejecutar sin verificación:
java -Xverify:none -cp . TestVillain
The end.
Exception in thread "main"