debugjavaCritical
Uncatchable ChuckNorrisException
Viewed 0 times
uncatchablechucknorrisexceptionstackoverflow
Problem
Is it possible to construct a snippet of code in Java that would make a hypothetical
Thoughts that came to mind are using for example interceptors or aspect-oriented programming.
java.lang.ChuckNorrisException uncatchable?Thoughts that came to mind are using for example interceptors or aspect-oriented programming.
Solution
I haven't tried this, so I don't know if the JVM would restrict something like this, but maybe you could compile code which throws
UPDATE:
It doesn't work. It generates a verifier error:
UPDATE 2:
Actually, you can get this to work if you disable the byte code verifier! (
UPDATE 3:
For those following from home, here is the full script:
Create the following classes:
Compile classes:
Run:
Comment out "extends RuntimeException" and recompile
Run:
Run without verification:
ChuckNorrisException, but at runtime provide a class definition of ChuckNorrisException which does not extend Throwable.UPDATE:
It doesn't work. It generates a verifier error:
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.UPDATE 2:
Actually, you can get this to work if you disable the byte code verifier! (
-Xverify:none)UPDATE 3:
For those following from home, here is the full script:
Create the following classes:
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.");
}
}
}Compile classes:
javac -cp . TestVillain.java ChuckNorrisException.javaRun:
java -cp . TestVillain
Gotcha!
The end.Comment out "extends RuntimeException" and recompile
ChuckNorrisException.java only :javac -cp . ChuckNorrisException.javaRun:
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.Run without verification:
java -Xverify:none -cp . TestVillain
The end.
Exception in thread "main"Code Snippets
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.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.");
}
}
}javac -cp . TestVillain.java ChuckNorrisException.javajava -cp . TestVillain
Gotcha!
The end.javac -cp . ChuckNorrisException.javaContext
Stack Overflow Q#13883166, score: 320
Revisions (0)
No revisions yet.