How does scala generated byte code drops the checked exception?

If it possible to write byte code for a method that is supposed to throw a checked exception?

For instance the following Java class doesn't compile unless the method declares it throws the checked exception:

public class CheckedExceptionJava {
  public Class<?> testChecked(String s) throws ClassNotFoundException {
    return Class.forName(s);
  }
}

While the following Scala equivalent does ( because Scala doesn't have checked exceptions ) :

class CheckedException { 
    def testChecked( s : String )  = Class.forName( s ) 
}

Even if the bytecode generated are almost identical:

Compiled from "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 from "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: Is it possible ( and how ) to generate bytecode for that doesn't mark it throws a checked exception even if the code inside that method doesn't handle it?

Answers


Simple. While the JVM bytecode includes checked exception specifications on methods, the bytecode verifier that runs before any bytecode is executed specifically doesn't check that the methods actually conform to exception specifications. You could write a program that took existing JVM bytecode and removed all exception specifications, and the resulting bytecode would be perfectly valid and run identically to the original (barring reflection).


Need Your Help

firing a jQuery change function at document ready

jquery initialization onchange

My change function allows users to switch from country to country and get different text and features. It works when changing country selections. But at initial page load, it does not fire jQuery c...

Declaring a dynamic array in C?

c arrays data-structures

I have a struct PACKET which contains two int fields. How would I declare a dynamic array of PACKETs? I will be adding to the array an unknown number of times in the main...that is why I need it to...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.