none
Why are my catches not catching? RRS feed

  • Question

  • Hello,

    I'm using Visual Studio 2017 Community to test a .NetCoreApp 1.1 console application from the second example(class to communicate with a server) located at this article.

    From this code:

    using System;
    using System.Collections;
    using System.Net;
    using System.Net.Security;
    using System.Net.Sockets;
    using System.Security.Authentication;
    using System.Text;
    using System.Security.Cryptography.X509Certificates;
    using System.IO;
    using Hashtable = System.Collections.Generic.Dictionary<object, object>;
    
    namespace Examples.System.Net
    {
         public class SslTcpClient
         {
              private static Hashtable certificateErrors = new Hashtable();
    
              public static bool ValidateServerCertificate(
                    object sender,
                    X509Certificate certificate,
                    X509Chain chain,
                    SslPolicyErrors sslPolicyErrors)
              {
                   if (sslPolicyErrors == SslPolicyErrors.None)
                        return true;
    
                   Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
    
                   return false;
              }
              public static void RunClient(string machineName, string serverName)
              {
                   TcpClient client = new TcpClient();
                   try
                   {
                        client.ConnectAsync(machineName, 443);
                   }
                   catch (ArgumentNullException anex)
                   {
                        Console.WriteLine("Exception: {0}", anex.Message);
                        if (anex.InnerException != null)
                        {
                             Console.WriteLine("Inner exception: {0}", anex.InnerException.Message);
                        }
                        Console.WriteLine("Connect Async failed - closing the connection.");
                        client.Dispose();
                        return;
                   }
                   catch (ArgumentOutOfRangeException aorex)
                   {
                        Console.WriteLine("Exception: {0}", aorex.Message);
                        if (aorex.InnerException != null)
                        {
                             Console.WriteLine("Inner exception: {0}", aorex.InnerException.Message);
                        }
                        Console.WriteLine("Connect Async failed - closing the connection.");
                        client.Dispose();
                        return;
                   }
                   catch (SocketException soex)
                   {
                        Console.WriteLine("Exception: {0}", soex.Message);
                        if (soex.InnerException != null)
                        {
                             Console.WriteLine("Inner exception: {0}", soex.InnerException.Message);
                        }
                        Console.WriteLine("Connect Async failed - closing the connection.");
                        client.Dispose();
                        return;
                   }
                   catch (ObjectDisposedException odex)
                   {
                        Console.WriteLine("Exception: {0}", odex.Message);
                        if (odex.InnerException != null)
                        {
                             Console.WriteLine("Inner exception: {0}", odex.InnerException.Message);
                        }
                        Console.WriteLine("Connect Async failed - closing the connection.");
                        client.Dispose();
                        return;
                   }
                   catch (Exception ex)
                   {
                        Console.WriteLine("Exception: {0}", ex.Message);
                        if (ex.InnerException != null)
                        {
                             Console.WriteLine("Inner exception: {0}", ex.InnerException.Message);
                        }
                        Console.WriteLine("Connect Async failed - closing the connection.");
                        client.Dispose();
                        return;
                   }
                   finally
                   {
                        Console.WriteLine("Client connected.");
    
                   }
    
                   SslStream sslStream = new SslStream(
                       client.GetStream(),
                       false,
                       new RemoteCertificateValidationCallback(ValidateServerCertificate),
                       null
                       );
                   
                   try
                   {
                        sslStream.AuthenticateAsClientAsync(serverName);
                   }
                   catch (AuthenticationException e)
                   {
                        Console.WriteLine("Exception: {0}", e.Message);
                        if (e.InnerException != null)
                        {
                             Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
                        }
                        Console.WriteLine("Authentication failed - closing the connection.");
                        //client.Close();
                        client.Dispose();
                        return;
                   }
                   catch (InvalidOperationException e)
                   {
                        Console.WriteLine("Exception: {0}", e.Message);
                        if (e.InnerException != null)
                        {
                             Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
                        }
                        Console.WriteLine("Authentication failed - closing the connection.");
                        //client.Close();
                        client.Dispose();
                        return;
                   }
                   catch (Exception e)
                   {
                        Console.WriteLine("Exception: {0}", e.Message);
                        if (e.InnerException != null)
                        {
                             Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
                        }
                        Console.WriteLine("Authentication failed - closing the connection.");
                        //client.Close();
                        client.Dispose();
                        return;
                   }
    


    The first Try returns, but there is no connection, yet it never executes any of the Catches.  This article shows all of the exceptions that can be generated by the client.ConnectAsync(machineName, 443); statement.

    It does execute the Finally.

    I also have the same problem with the second Try.  None of the catches execute when the SslStream statement executes.

    Any help would be gratefully appreciated.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Thursday, April 27, 2017 7:07 PM

Answers

  • This is caused by async function.

    For catching the exception, you could try below code:

    client.ConnectAsync(machineName, 443).Wait();

    For more information about Async/awit, I suggest you refer below link.

    #Exceptions from an Async Void Method Can’t Be Caught with Catch

    https://msdn.microsoft.com/en-us/magazine/jj991977.aspx?f=255&MSPPError=-2147217396


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Carneno Saturday, April 29, 2017 3:10 AM
    Friday, April 28, 2017 5:21 AM
    Moderator

All replies

  • This is caused by async function.

    For catching the exception, you could try below code:

    client.ConnectAsync(machineName, 443).Wait();

    For more information about Async/awit, I suggest you refer below link.

    #Exceptions from an Async Void Method Can’t Be Caught with Catch

    https://msdn.microsoft.com/en-us/magazine/jj991977.aspx?f=255&MSPPError=-2147217396


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Carneno Saturday, April 29, 2017 3:10 AM
    Friday, April 28, 2017 5:21 AM
    Moderator
  • Edward,

    Yes, that worked.

    Al I need to do now is read and learn something about Async programming.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Saturday, April 29, 2017 3:09 AM