Troubles with WinAPI in X++

Maybe the title is some kind of misguiding, since it's neither the WinAPI nor X++ that are causing troubles, but the wrong use of the WinAPI with X++. Anyway, you might know what I mean...

Following blindly sample code from the web or the online documentation isn't always a good idea, especially if you like to use the WinAPI. Here a sample-code that causes often troubles:

    1:  int winHandle; 
    2:   
    3:  container c; 
    4:  ; 
    5:  ... 
    6:   
    7:  c = WinAPI::findFirstFile( ... ); 
    8:  winHandle = conpeek(c,1); 
    9:  if ( winHandle != #INVALID_HANDLE_VALUE) 
   10:  { 
   11:      // something to do here... 
   12:      ... your code ... 
   13:      WinAPI::closeHandle(winHandle);  
   14:  }

Well, normally everything works fine, but once your code throwing an exception the handle isn't closed and the file stays locked until you restart your application. In order to prevent such unnecessary problems, please don't follow wrong examples that you might find on the web (or your product documentation) and close all unmanaged resources in any case and this includes the case of an exception.

    1:  c = WinAPI::findFirstFile( ... ); 
    2:  winHandle = conpeek(c,1); 
    3:  if ( winHandle != #INVALID_HANDLE_VALUE) 
    4:  { 
    5:     try 
    6:     {
    7:   
    8:          // something to do here... 
    9:         ... your code ... 
   10:         WinAPI::closeHandle(winHandle); //if everything goes fine 
   11:     } 
   12:     catch  
   13:     { 
   14:         WinAPI::closeHandle(winHandle); //and otherwise, too! 
   15:     }
   16:   
   17:  }

Well, I would agree that a language like X++ should know a the try-finally like C# does. The finally block is executed in any case - exception or not, which would simplify the code above quite a lot and prevent stupid bugs in your code. The code above with a finally block would look like this:

    1:  c = WinAPI::findFirstFile( ... ); 
    2:  winHandle = conpeek(c,1); 
    3:  if ( winHandle != #INVALID_HANDLE_VALUE) 
    4:  { 
    5:     try 
    6:     {
    7:   
    8:          // something to do here... 
    9:         ... your code ... 
   10:      } 
   11:     finally 
   12:     { 
   13:         WinAPI::closeHandle(winHandle); 
   14:     } 
   15:  }