locked
Exception not being caught

    Question

  • I have a situation where if the user is specifying an invalid host name to be evaluated in my application a InvalidArgumentException is being thrown. I've attempted to catch this exception in order to create a proper response back to the user, but for some reason my exception is never being caught. Below is a sample of the code that I'm attempting to use:

    	HostName^ hostName = nullptr;
    
    	try
    	{
    		hostName = ref new HostName(ref new Platform::String(tbxHostName->Text->Data()));
    	}
    	catch(Platform::InvalidArgumentException^ e)
    	{
    		this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]()
    		{
    			this->tbMessage->Text = L"Invalid host name";
    		}));
    	}

    The exception is never caught by my catch clause.

    I've made sure that Platform::InvalidArgumentException is enabled to be caught in the debugger configuration.

     
    Sunday, April 07, 2013 9:08 PM

Answers

  • It appears to throw an InvalidArgumentException followed by a COMException. Try:

    	HostName^ hostName = nullptr;
    	try
    	{
    		try
    		{
    			hostName = ref new HostName(ref new Platform::String(tbxHostName->Text->Data()));
    		}
    		catch (Platform::Exception^ ex)
    		{
    			this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]()
    			{
    				this->tbMessage->Text = L"Invalid host name";
    			}));
    		}
    	}
    	catch (Platform::Exception^ e)
    	{
    		this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]()
    		{
    			this->tbMessage->Text = L"Invalid host name";
    		}));
    	}
    


    Visual C++ MVP | Website | Blog | @mikebmcl | Windows Store DirectX Game Template

    Monday, April 08, 2013 4:28 PM

All replies

  • Hi l3v3l6<abbr class="affil"></abbr>,

    -> Your Platform::InvalidArgumentException will be caught only when your hostName contains nothing.

    -> In spite of adding try-catch during hostName creation you have to handle the exception in subsequent APIs call like in ConnectAsync(). So I think you should handle it there.

    -> If your hostName contain invalid host name (which will be generate "No such host is known." exception during the execution of ConnectAsync()), then you have to handle the exception in subsequent ConnectAsync() and take necessary steps.

    You can give idea from following code snapped.

    HostName^ hostName = nullptr;
    hostName = ref new HostName(ref new Platform::String(tbxHostName->Text->Data()));
    if (nullptr == hostName ){
           // Handle hrere.........
    }
    
    task<void>(socket->ConnectAsync(hostName, "80")).then([&asyncOperationCompleted] (task<void> previousTask) { 
    			try{
    				previousTask.get(); 				
    				OutputDebugStringW(L"\n### Connection Success\n");
    			} 
    			catch(Platform::InvalidArgumentException^ ex){
    				OutputDebugStringW(ex->Message->Data());
    			}
    			catch( Platform::Exception^ ex ) { 
    				OutputDebugStringW(ex->Message->Data());
    			}
    		});


    • Proposed as answer by MikeBMcLMVP Monday, April 08, 2013 4:44 AM
    Monday, April 08, 2013 4:10 AM
  • The exception is also occurring when Hostname is instantiated using a name with illegal characters, such as "Bob's MacBook". This happens before I have a chance to call ConnectAsynch or even check to see if HostName is null. So, how can I trap an exception in the Hostname constructor?
    Monday, April 08, 2013 1:33 PM
  • Hi,

    Please notice the such as "Bob's MacBook" that you mentioned. You can see that here is a special/illegal character " ' " even it is not a dotted domain name in the desired format. When you pass something through HostName() constructor, it is expected that it is not nullptr and a typical domain name in the specified format or a dotted valid IP address.

    Now, suppose in an API we are expecting dotted valid IP address for some processing and user may give the IP address wrongly. In this case, what we usually do? Of course, we go through a function like is_dotted_valid_ip_address() which checks the provided ip address is valid or not before further processing. Similarly, here you may write a function like is_valid_domain_name() which will check whether the provided hostName is valid or not through typical string processing before constructing HostName. If the given string is valid HostName then you go ahead for constructing the HostName or calling ConnectAsync.

    Monday, April 08, 2013 4:11 PM
  • Thanks for your reply. I understand all that you are saying and I'm well aware that "Bob's MacBook" is not a valid host name. But, you'd be surprised what users type in as their computer name. From a Mac perspective "Bob's MacBook" is a valid computer name and will resolve using a name service like Bonjour. If the HostName constructor is already creating an exception when passed an invalid parameter, why can't I just trap on that exception, rather than creating a bunch of user functions that check the validity of the parameter. 

    I just want to trap the InvalidArgumentException and that does not seem to be working as expected.

    Monday, April 08, 2013 4:26 PM
  • It appears to throw an InvalidArgumentException followed by a COMException. Try:

    	HostName^ hostName = nullptr;
    	try
    	{
    		try
    		{
    			hostName = ref new HostName(ref new Platform::String(tbxHostName->Text->Data()));
    		}
    		catch (Platform::Exception^ ex)
    		{
    			this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]()
    			{
    				this->tbMessage->Text = L"Invalid host name";
    			}));
    		}
    	}
    	catch (Platform::Exception^ e)
    	{
    		this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]()
    		{
    			this->tbMessage->Text = L"Invalid host name";
    		}));
    	}
    


    Visual C++ MVP | Website | Blog | @mikebmcl | Windows Store DirectX Game Template

    Monday, April 08, 2013 4:28 PM