title: How to I catch SSL exceptions in a Mono HTTPListener server?
body: <p>I'm trying to add HTTPS support to my simple Mono HTTP server. It works perfectly most of the time, but the application crashes when an SSL exception occurs, such as the client canceling the handshake. Obviously something like that shouldn't completely crash it, so I need to catch that exception somehow.</p>
  
  <p>Code:</p>
  
  <pre><code>    static void Main(string[] args)
      {
          try
          {
              var listener = new HttpListener();
              string prefix = "https://*:8443/";
              listener.Prefixes.Add(prefix);
  
              Console.WriteLine("Starting HTTP server at " + prefix);
  
              listener.Start();
  
              while (true)
              {
                  try
                  {
                      var context = listener.GetContext();
                      Console.WriteLine(context.Request.Url);
                  }
                  catch (Exception e)
                  {
                      Console.ForegroundColor = ConsoleColor.Red;
                      Console.WriteLine("Exception processing request: " + e);
                      Console.ResetColor();
                  }
              }
          }
          catch (Exception e)
          {
              Console.ForegroundColor = ConsoleColor.Yellow;
              Console.WriteLine("Exception caught! " + e);
          }
      }
  </code></pre>
  
  <p>Here's the log when I get a wonky client trying to connect:</p>
  
  <pre><code>Starting HTTP server at https://*:8443/
  
  Unhandled Exception:
  System.IO.IOException: The authentication or decryption has failed. ---&gt; Mono.Security.Protocol.Tls.TlsException: The client stopped the handshake.
    at Mono.Security.Protocol.Tls.SslServerStream.EndNegotiateHandshake (IAsyncResult asyncResult) &lt;0x41e581b0 + 0x0022b&gt; in &lt;filename unknown&gt;:0
    at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) &lt;0x41e57d20 + 0x00086&gt; in &lt;filename unknown&gt;:0
    --- End of inner exception stack trace ---
    at Mono.Security.Protocol.Tls.SslStreamBase.EndRead (IAsyncResult asyncResult) &lt;0x41e50110 + 0x0015f&gt; in &lt;filename unknown&gt;:0
    at Mono.Net.Security.Private.LegacySslStream.EndAuthenticateAsServer (IAsyncResult asyncResult) &lt;0x41e50000 + 0x0003e&gt; in &lt;filename unknown&gt;:0
    at Mono.Net.Security.Private.LegacySslStream.AuthenticateAsServer (System.Security.Cryptography.X509Certificates.X509Certificate serverCertificate, Boolean clientCertificateRequired, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation) &lt;0x41e40990 + 0x00055&gt; in &lt;filename unknown&gt;:0
    at System.Net.HttpConnection.Init () &lt;0x41e3e0e0 + 0x0005f&gt; in &lt;filename unknown&gt;:0
    at System.Net.HttpConnection..ctor (System.Net.Sockets.Socket sock, System.Net.EndPointListener epl, Boolean secure, System.Security.Cryptography.X509Certificates.X509Certificate cert) &lt;0x41e3b670 + 0x003e7&gt; in &lt;filename unknown&gt;:0
    at System.Net.EndPointListener.OnAccept (System.Object sender, System.EventArgs e) &lt;0x41e3b210 + 0x002a7&gt; in &lt;filename unknown&gt;:0
    at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted (System.Net.Sockets.SocketAsyncEventArgs e) &lt;0x41e3b1d0 + 0x0002e&gt; in &lt;filename unknown&gt;:0
    at System.Net.Sockets.SocketAsyncEventArgs.Complete () &lt;0x41e3b1b0 + 0x00013&gt; in &lt;filename unknown&gt;:0
    at System.Net.Sockets.Socket.&lt;AcceptAsyncCallback&gt;m__0 (IAsyncResult ares) &lt;0x41e3aa70 + 0x0037f&gt; in &lt;filename unknown&gt;:0
    at System.Net.Sockets.SocketAsyncResult+&lt;Complete&gt;c__AnonStorey0.&lt;&gt;m__0 (System.Object _) &lt;0x41e3a980 + 0x0001d&gt; in &lt;filename unknown&gt;:0
    at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () &lt;0x7f22cac25460 + 0x0002f&gt; in &lt;filename unknown&gt;:0
    at System.Threading.ThreadPoolWorkQueue.Dispatch () &lt;0x7f22cac239e0 + 0x001d6&gt; in &lt;filename unknown&gt;:0
    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () &lt;0x7f22cac252e0 + 0x00008&gt; in &lt;filename unknown&gt;:0
  </code></pre>