How does scala generated bytecode throw an exception?

If it is possible to write bytecode for a method that should throw a checked exception?

For example, the following Java class does not compile unless the method declares that it has thrown an exception.

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

So far, the following Scala equivalent has been used (since Scala has no checked exceptions):

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

Even if the generated bytecode is 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 this without marking it, throws an exception excepted, even if the code inside this method does not handle it?

+5
source share
1 answer

Simple. - JVM , -, , - - , , . , - JVM, , - ( ).

+13

All Articles