Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here you are catching <code>Error</code> and not <code>Exception</code> in which case your program would have crashed.</p> <p>If you try this code (modified to add a static counter)</p> <pre><code>public class StackError { static int i = 1; public static void main(final String[] args) throws Exception { System.out.print("1"); doAnything(); System.out.println("2"); } private static void doAnything() { try { i++; // System.out.println(i); doAnything(); } catch (Error e) { System.out.print("y"+i+"-"); } } } </code></pre> <h3>Output</h3> <pre><code> 1y6869-2 </code></pre> <p>So, it has got <code>stackerror</code> 6869 times(changes for different runs) and the last value is printed. If you just print the <code>y</code> as you did earlier then it might the case that the output is getting bufferred and not getting flushed as it is not a <code>println</code>.</p> <hr> <h3>Update</h3> <p>The <code>System.out.println</code> internally calls the <code>PrintStream</code> which is buffered. You don't loose any data from the buffer, it gets all written to the output( terminal in your case) after it fills up, or when you explicitly call flush on it. </p> <p>Coming back to this scenario, it depends on the internal dynamics of how much the stack is filled up and how many print statements were able to get executed from the catch in <code>doAnything()</code> and those number of characters were written to the buffer. In the main back it finnally get's printed with the number <code>2</code>.</p> <p><a href="http://docs.oracle.com/javase/tutorial/essential/io/buffers.html" rel="noreferrer">javadoc reference to buffered streams</a></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. 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.
 

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