1

I was browsing the sources of .Net Framework 4.7.1 System.Diagnostics.Process and found this construct (line 2114ff).

I never had the impetous to put someting in a finally just for the sake of it - I use it for things that neeed to happen even if exceptions (handled/unhandled) in the try { ... } part arise.

The try-catch-finally docs tell about try, catch and finally - but nothing about this construct with an emtpy try. What is the purpose of the

try
{
    // empty
}
finallly
{
    // do something
}

code block?


Quote:

// https://referencesource.microsoft.com/#System/services/monitoring/system/diagnosticts/Process.cs,2114  

RuntimeHelpers.PrepareConstrainedRegions();
try {} finally {
   retVal = NativeMethods.CreateProcess (
           null,               // we don't need this since all the info is in commandLine
           commandLine,        // pointer to the command line string
           null,               // pointer to process security attributes, we don't need to inheriat the handle
           null,               // pointer to thread security attributes
           true,               // handle inheritance flag
           creationFlags,      // creation flags
           environmentPtr,     // pointer to new environment block
           workingDirectory,   // pointer to current directory name
           startupInfo,        // pointer to STARTUPINFO
           processInfo         // pointer to PROCESS_INFORMATION
       );
   if (!retVal)                            
          errorCode = Marshal.GetLastWin32Error();
   if ( processInfo.hProcess!= (IntPtr)0 && processInfo.hProcess!= (IntPtr)NativeMethods.INVALID_HANDLE_VALUE)
       procSH.InitialSetHandle(processInfo.hProcess);  
   if ( processInfo.hThread != (IntPtr)0 && processInfo.hThread != (IntPtr)NativeMethods.INVALID_HANDLE_VALUE)
      threadSH.InitialSetHandle(processInfo.hThread);                    
}

I took a peek into Why is try {...} finally {...} good; try {...} catch{} bad? but could not find anything related to this.

Community
  • 1
  • 1
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69

0 Answers0