title: C++/CLI try catch handling
body: <p>Below is the code I am using to communicate with my arduino on windows xp.  The problem I am having is when I have two commands trying to access the port at the same time, which is the UnauthroizedAccessException catch statement, it outputs the errormessage, and doesn't execute one of the commands, How could I go about coding that catch statement so that instead of catching the error or even making it to the error for the program to finish the first command and then execute with other one, something like a queue....</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  
  using namespace System;
  using namespace System::IO::Ports;
  
  int main(int argc, char* argv[])
  {
      using namespace std;
  
      String^ portName;
      int baudRate=9600;
  
      portName="COM4";
      // arduino settings
      SerialPort^ arduino;
  
      arduino = gcnew SerialPort(portName, baudRate);
      // open port
      try
      {
          arduino-&gt;Open();
          {
              if(strcmp(argv[1],"-send")==0){
                  String^ command = gcnew String(reinterpret_cast&lt;const char*&gt;(argv[2]));
  
                  if(String::Compare(command,"int6")==0){
                      arduino-&gt;Write("^");
                  }else
                      arduino-&gt;Write(command);
              }
              if(strcmp(argv[1],"-get")==0){
                  String^ command = gcnew String(reinterpret_cast&lt;const char*&gt;(argv[2]));
                  arduino-&gt;ReadTimeout = 1000;        
                  arduino-&gt;WriteLine(command);
  
                  String^ result = arduino-&gt;ReadLine();
  
                  Console::Write(result);
              }
          }
          // close port to arduino
          arduino-&gt;Close();
      }
      catch (IO::IOException^ e){
          Console::Write("errormessagedisconnected");
      }
      catch (TimeoutException^ e){
          Console::Write("errormessage");
      }
      catch (UnauthorizedAccessException^ e){
          Console::Write("errormessage");
      }
      catch (ArgumentException^ e){
          Console::WriteLine(e-&gt;GetType()-&gt;Name+": incorrect port name syntax, must start with COM/com");
      }
      // end program
  
      return 0;
  }
  </code></pre>