Although this doesn't check in advance you can check if the service exists when you call the service.
You need to trap the error but the normal try/catch inline won't capture the error because the error generated is at the application level.
You have to capture the error at the Application Level
1 - Create a global in your XAML Page.cs to hold any error you with to pass eg:
public partial class MainPage : UserControl
{
string _strUnhandledException = "";
:
:
2 - Where you are dynamically adding your layer preload the variable with any message you might wish to display eg:
ArcGISTiledMapServiceLayer TiledLayer = new ArcGISTiledMapServiceLayer();
TiledLayer.ID = larrValues[0];
TiledLayer.Url = larrValues[1];
_strUnhandledException = larrValues[0] + "\n" + larrValues[1]; // «««««««««
MyMap.Layers.Add(TiledLayer);
3 - In the Application XAML App.xaml.cs go to the event:
private void Application_UnhandledException
// Change the code to the following:
if (!System.Diagnostics.Debugger.IsAttached)
{
e.Handled = true;
if (_strUnhandledException != "") {ReportCustomError(e);} // « new line
else
{
Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
}
}
4 - In the Application XAML App.xaml.cs add this function:
private void ReportCustomError(ApplicationUnhandledExceptionEventArgs e)
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
errorMsg = "Application Unhandled Exception:"
+ "\n***************************"
+ "\n" + e.ToString()
+ "\n" + _strUnhandledException
+ "\n\n" + errorMsg;
MessageBox.Show(errorMsg);
_strUnhandledException = "";
return;
}
catch (Exception)
{
}
}