Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tricky try-catch java code

Tags:

java

public class Strange1 {
  public static void main(String[] args) {
    try {
      Missing m = new Missing();
    } catch (java.lang.NoClassDefFoundError ex) {
      System.out.println("Got it!");
    }
  }
}

public class Strange2 {
  public static void main(String[] args) {
    Missing m;
    try {
      m = new Missing();
    } catch (java.lang.NoClassDefFoundError ex) {
      System.out.println("Got it!");
    }
  }
}

class Missing {
  Missing() { }
}

If you run Strange1 and Strange2 after deleting Missing.class, Strange1 will throw NoClassDefFoundError; but Strange2 will print Got it!

Can anyone explain that? Thanks.

updated:

java bytecode for Strange1 :

     0  new info.liuxuan.test.Missing [16]
     3  dup
     4  invokespecial info.liuxuan.test.Missing() [18]
     7  astore_1 [m]
     8  goto 20
    11  astore_1 [ex]
    12  getstatic java.lang.System.out : java.io.PrintStream [19]
    15  ldc <String "Got it!"> [25]
    17  invokevirtual java.io.PrintStream.println(java.lang.String) : void [27]
    20  return
      Exception Table:
        [pc: 0, pc: 8] -> 11 when : java.lang.NoClassDefFoundError
      Line numbers:
        [pc: 0, line: 14]
        [pc: 11, line: 15]
        [pc: 12, line: 16]
        [pc: 20, line: 18]
      Local variable table:
        [pc: 0, pc: 21] local: args index: 0 type: java.lang.String[]
        [pc: 8, pc: 11] local: m index: 1 type: info.liuxuan.test.Missing
        [pc: 12, pc: 20] local: ex index: 1 type: java.lang.NoClassDefFoundError

java bytecode for Strange2 :

     0  new info.liuxuan.test.Missing [16]
     3  dup
     4  invokespecial info.liuxuan.test.Missing() [18]
     7  astore_1 [m]
     8  goto 20
    11  astore_2 [ex]
    12  getstatic java.lang.System.out : java.io.PrintStream [19]
    15  ldc <String "Got it!"> [25]
    17  invokevirtual java.io.PrintStream.println(java.lang.String) : void [27]
    20  return
      Exception Table:
        [pc: 0, pc: 8] -> 11 when : java.lang.NoClassDefFoundError
      Line numbers:
        [pc: 0, line: 15]
        [pc: 11, line: 16]
        [pc: 12, line: 17]
        [pc: 20, line: 19]
      Local variable table:
        [pc: 0, pc: 21] local: args index: 0 type: java.lang.String[]
        [pc: 8, pc: 11] local: m index: 1 type: info.liuxuan.test.Missing
        [pc: 12, pc: 20] local: ex index: 2 type: java.lang.NoClassDefFoundError

There is only one place is different:

11  astore_1 [ex]

and

11  astore_2 [ex]

updated again:

Everyone can try it in eclipse.

like image 556
Foredoomed Avatar asked Aug 09 '11 04:08

Foredoomed


People also ask

What is Java Trycatch?

Java try and catchThe try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

Are try-catch blocks good practice?

It's considered best practice to put as little code as possible in each try / catch block. This means that you may need multiple try / catch blocks, instead of just one. The benefits of this are that: it's easy to see which code raises which exceptions (and which code doesn't raise exceptions)

Is try with multiple catch allowed?

Yes, we can define one try block with multiple catch blocks in Java. Every try should and must be associated with at least one catch block.


2 Answers

Prior to saying anything, i doub't this code won't even compile. because when compiler cannot find a class (Since its deleted). may be you are getting an error when trying to compile it using javac command. if thats the case its pretty normal and in no way its weird.

and let me add an another point.. check your imports, to contain Missing class. if it is there then remove it. and tell us whats happening.

like image 63
ngesh Avatar answered Oct 12 '22 13:10

ngesh


I created two java files. Strange1.java contained classes Strange1 and Missing. Strange2.java contained Strange2 class. I removed Missing.class. I got "Got it!" from both.

Please see the following details:

manohar@manohar-natty:~$ java -version
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) Server VM (build 20.0-b11, mixed mode)
manohar@manohar-natty:~$ gedit Strange1.java
manohar@manohar-natty:~$ gedit Strange2.java
manohar@manohar-natty:~$ javac Strange1.java 
manohar@manohar-natty:~$ javac Strange2.java 
manohar@manohar-natty:~$ java Strange1
manohar@manohar-natty:~$ java Strange2
manohar@manohar-natty:~$ rm Missing.class
manohar@manohar-natty:~$ java Strange1
Got it!
manohar@manohar-natty:~$ java Strange2
Got it!

I executed it in Ubuntu 11.04 linux machine.

So it might be the java's version that you are using.

like image 28
Manohar Bhattarai Avatar answered Oct 12 '22 14:10

Manohar Bhattarai