java - How does scala generated byte code drops the checked exception? -
if possible write byte code method supposed throw checked exception?
for instance following java class doesn't compile unless method declares throws checked exception:
public class checkedexceptionjava { public class<?> testchecked(string s) throws classnotfoundexception { return class.forname(s); } } while following scala equivalent ( because scala doesn't have checked exceptions ) :
class checkedexception { def testchecked( s : string ) = class.forname( s ) } even if bytecode generated identical:
compiled "checkedexceptionjava.java" public class checkedexceptionjava extends java.lang.object{ public checkedexceptionjava(); code: 0: aload_0 1: invokespecial #1; //method java/lang/object."<init>":()v 4: return public java.lang.class testchecked(java.lang.string) throws java.lang.classnotfoundexception; code: 0: aload_1 1: invokestatic #2; //method java/lang/class.forname:(ljava/lang/string;)ljava/lang/class; 4: areturn } compiled "checkedexception.scala" public class checkedexception extends java.lang.object implements scala.scalaobject{ public checkedexception(); code: 0: aload_0 1: invokespecial #24; //method java/lang/object."<init>":()v 4: return public java.lang.class testchecked(java.lang.string); code: 0: aload_1 1: invokestatic #11; //method java/lang/class.forname:(ljava/lang/string;)ljava/lang/class; 4: areturn } question: possible ( , how ) generate bytecode doesn't mark throws checked exception if code inside method doesn't handle it?
simple. while jvm bytecode includes checked exception specifications on methods, bytecode verifier runs before bytecode executed doesn't check methods conform exception specifications. write program took existing jvm bytecode , removed exception specifications, , resulting bytecode valid , run identically original (barring reflection).
Comments
Post a Comment