Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the victim of bad recursive call. As you are wondering why the value of <em>cnt</em> varies, it is because the stack size depends on the platform. Java SE 6 on Windows has a default stack size of 320k in the 32-bit VM and 1024k in the 64-bit VM. You can read more <a href="http://www.oracle.com/technetwork/java/hotspotfaq-138619.html" rel="noreferrer">here</a>.</p> <p>You can run using different stack sizes and you will see different values of <em>cnt</em> before the stack overflows-</p> <blockquote> <p>java -Xss1024k RandomNumberGenerator</p> </blockquote> <p>You don't see the value of <em>cnt</em> being printed multiple times even though the value is greater than 1 sometimes because your print statement is also throwing error which you can debug to be sure through Eclipse or other IDEs.</p> <p>You can change the code to the following to debug per statement execution if you'd prefer-</p> <pre><code>static int cnt = 0; public static void main(String[] args) { try { main(args); } catch (Throwable ignore) { cnt++; try { System.out.println(cnt); } catch (Throwable t) { } } } </code></pre> <p><strong>UPDATE:</strong></p> <p>As this getting a lot more attention, let's have another example to make things clearer-</p> <pre><code>static int cnt = 0; public static void overflow(){ try { overflow(); } catch (Throwable t) { cnt++; } } public static void main(String[] args) { overflow(); System.out.println(cnt); } </code></pre> <p>We created another method named <em>overflow</em> to do a bad recursion and removed the <em>println</em> statement from the catch block so it doesn't start throwing another set of errors while trying to print. This works as expected. You can try putting <em>System.out.println(cnt);</em> statement after <em>cnt++</em> above and compile. Then run multiple times. Depending on your platform, you may get different values of <em>cnt</em>.</p> <p>This is why generally we do not catch errors because mystery in code is not fantasy.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload