jason
jason

Reputation: 65

Scalac compiler doesn't create a .class file

I am trying understand the error handling in scala.

I compiled the code as following

scalac sampleExceptionHandling.sc

I thought it would create a .class file and using javap command, I can look at the equivalent java code. For somereason, .class file isn't created. And no errors thrown either after executing Scalac command.

Any suggestion please?

 import java.io.FileReader
 import java.io.FileNotFoundException
 import java.io.IOException

object Demo {
  def main(args: Array[String]) {
  try {
     val f = new FileReader("input.txt")
  } catch {
     case ex: FileNotFoundException =>{
        println("Missing file exception")
     }

     case ex: IOException => {
        println("IO Exception")
     }
     }
   }
 }

Upvotes: 0

Views: 511

Answers (1)

prayagupadhyay
prayagupadhyay

Reputation: 31232

It does create .class which is same as your class name not as your filename.

Following is the result of scalac sampleExceptionHandling.sc

$ ll
total 24
10909194 -rw-r--r--  1 as18  prayagupd  936 Mar 15 10:00 Demo$.class
10909193 -rw-r--r--  1 as18  prayagupd  565 Mar 15 10:00 Demo.class
10909167 -rw-r--r--  1 as18  prayagupd  378 Mar 15 09:59 sampleExceptionHandling.sc

To run the class

$ scala Demo
Missing file exception

To see the bytecode,

$ javap -c Demo.class 
Compiled from "sampleExceptionHandling.sc"
public final class Demo {
  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #16                 // Field Demo$.MODULE$:LDemo$;
       3: aload_0
       4: invokevirtual #18                 // Method Demo$.main:([Ljava/lang/String;)V
       7: return
}

And,

$ javap -c Demo$.class 
Compiled from "sampleExceptionHandling.sc"
public final class Demo$ {
  public static final Demo$ MODULE$;

  public static {};
    Code:
       0: new           #2                  // class Demo$
       3: invokespecial #12                 // Method "<init>":()V
       6: return

  public void main(java.lang.String[]);
    Code:
       0: new           #20                 // class java/io/FileReader
       3: dup
       4: ldc           #22                 // String input.txt
       6: invokespecial #25                 // Method java/io/FileReader."<init>":(Ljava/lang/String;)V
       9: astore        4
      11: goto          35
      14: astore_2
      15: getstatic     #30                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
      18: ldc           #32                 // String IO Exception
      20: invokevirtual #36                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
      23: goto          35
      26: astore_3
      27: getstatic     #30                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
      30: ldc           #38                 // String Missing file exception
      32: invokevirtual #36                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
      35: return
    Exception table:
       from    to  target type
           0    14    26   Class java/io/FileNotFoundException
           0    14    14   Class java/io/IOException
}

Upvotes: 5

Related Questions