Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is what <a href="http://en.wikipedia.org/wiki/Exception_handling" rel="noreferrer">Wikipedia</a> says about finally clause:</p> <blockquote> <p>More common is a related clause (finally, or ensure) that is executed whether an exception occurred or not, typically to release resources acquired within the body of the exception-handling block.</p> </blockquote> <p>Let's dissect your program.</p> <pre><code>try { System.out.print(1); q(); } </code></pre> <p>So, <code>1</code> will be output into the screen, then <code>q()</code> is called. In <code>q()</code>, an exception is thrown. The exception is then caught by <code>Exception y</code> but it does nothing. A <strong>finally</strong> clause is then executed (it has to), so, <code>3</code> will be printed to screen. Because (in method <code>q()</code> there's an exception thrown in the <em>finally</em> clause, also <code>q()</code> method passes the exception to the parent stack (by the <code>throws Exception</code> in the method declaration) <code>new Exception()</code> will be thrown and caught by <code>catch ( Exception i )</code>, <code>MyExc2</code> exception will be thrown (for now add it to the exception stack), but a <em>finally</em> in the <code>main</code> block will be executed first.</p> <p>So in, </p> <pre><code>catch ( Exception i ) { throw( new MyExc2() ); } finally { System.out.print(2); throw( new MyExc1() ); } </code></pre> <p>A <strong>finally</strong> clause is called...(remember, we've just caught <code>Exception i</code> and thrown <code>MyExc2</code>) in essence, <code>2</code> is printed on screen...and after the <code>2</code> is printed on screen, a <code>MyExc1</code> exception is thrown. <code>MyExc1</code> is handled by the <code>public static void main(...)</code> method.</p> <p>Output:</p> <blockquote> <p>"132Exception in thread main MyExc1"</p> </blockquote> <p>Lecturer is correct! :-)</p> <p><em>In essence</em>, if you have a <strong>finally</strong> in a try/catch clause, a finally will be executed (<strong>after</strong> catching the exception <strong>before</strong> throwing the caught exception out)</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.
 

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