Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes. Even if there was an <code>Exception</code> within <code>catch</code> block, <code>finally</code> will be executed.</p> <p>If you are familiar with C++, just think <code>finally</code> as the <code>destructor</code> of an <code>object</code>. What ever the state of a statement within the object, <code>~Destructor</code> will be executed. But you cant put <code>return</code> within <code>finally</code>[some compilers allow though].</p> <p>See the code below: See how global variable <code>y</code> been changed. Also see how <code>Exception1</code> been covered by <code>Exception2</code>.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace finallyTest { class Program { static int y = 0; static int testFinally() { int x = 0; try { x = 1; throw new Exception("Exception1"); x = 2; return x; } catch (Exception e) { x = -1; throw new Exception("Exception2", e); } finally { x = 3; y = 1; } return x; } static void Main(string[] args) { try { Console.WriteLine("&gt;&gt;&gt;&gt;&gt;" + testFinally()); } catch (Exception e) { Console.WriteLine("&gt;&gt;&gt;&gt;&gt;" + e.ToString()); } Console.WriteLine("&gt;&gt;&gt;&gt;&gt;" + y); Console.ReadLine(); } } } </code></pre> <p>output:</p> <pre><code> &gt;&gt;&gt;&gt;&gt;System.Exception: Exception2 ---&gt; System.Exception: Exception1 at finallyTest.Program.testFinally() in \Projects\finallyTest\finallyTest\Program.cs:line 17 --- End of inner exception stack trace --- at finallyTest.Program.testFinally() in \Projects\finallyTest\finallyTest\Program.cs:line 24 at finallyTest.Program.Main(String[] args) in \Projects\finallyTest\finallyTest\Program.cs:line 38 &gt;&gt;&gt;&gt;&gt;1 </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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