0

suppose that i have this code. if exception is InvalidData, do this will call finally to clean resources.

while(CanWork){
            try
            {
                 ....
            }
            catch (InvalidDataException e)
            {
                LogAction(false, e.Message, e.StackTrace);
                break;
            }
            catch (Exception e)
            {
                LogAction(false, e.Message, e.StackTrace);
            }
            finally
            {
                if (insta != null)
                {
                    insta.Disconnect();
                    insta.Dispose();
                    insta = null;
                }
            }
          }
Daniel
  • 197
  • 2
  • 16

2 Answers2

4

Yes it will, but not when it is about to leave the while, but just after the code in the try or catch is done executing.

Max
  • 12,622
  • 16
  • 73
  • 101
1

Yes of course . finally block always gets executed . Please refer this link for more details

Pawan
  • 1,065
  • 5
  • 10
  • be careful with "always" - there are a range of scenarios where it won't be; ok, those are pretty pathological scenarios where the process is very sick - but they are genuine. More simply: if the power switches off it won't run `finally` – Marc Gravell Jun 20 '13 at 07:32
  • @MarcGravell Can you please let me know the all such range of scenarios or refer a link , it will be very very helpful for me – Pawan Jun 20 '13 at 07:35
  • 1
    scenarios involving thread-abort, or fast exit, possibly certain security exceptions or out-of-memory scenarios; things like that - the nasty ones – Marc Gravell Jun 20 '13 at 07:56