How to propagate exceptions?

449
4
04-25-2020 08:16 PM
AbelPerez
Occasional Contributor II

So I just started with a Pro add-in and have added a DockPane. The user fills in input and I send that to a geoprocessing function. In the DockPane I have a button with a click event

Private Sub btnGo_Click(sender As Object, e As RoutedEventArgs) Handles btnGo.Click
   Try

      'collect WPF form info

      DoGp(info)
   Catch ex As Exception
      MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error)
   End Try
End Sub

In a separate module I have

Public Async Sub DoGp(ByVal vg As Info)
Try

   Dim toolPath As String = "management.CreateFishnet"
   Dim progDlg As ProgressDialog = New ProgressDialog("Running Geoprocessing Tool", "Cancel", 100, True)
progDlg.Show()
   Dim progSrc As CancelableProgressorSource = New CancelableProgressorSource(progDlg)
   Dim res As IGPResult = Await Geoprocessing.ExecuteToolAsync(toolPath, params, envr, progSrc.Progressor, GPExecuteToolFlags.Default)
   progDlg.Hide()
   If res.IsFailed Then Throw New Exception("Geoprocessing tool failed.")

Catch ex As Exception
   Debug.Print(ex.ToString)
   Throw  'crashes here
End Try
End Sub

When I run it and there is an error in the output there is an unhandled exception at the line Throw:

An exception of type 'System.Exception' occurred in MyAddin.dll but was not handled in user code
Geoprocessing tool failed.

I have Try-Catch in the calling code and also in the module so why is it not propagated?

0 Kudos
4 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I have two suggestions:

1) In your calling code you have to 'await' the outcome of the function you call, currently you only 'initiate' the DoGp method and the call returns back to the calling method before the function is actually executed..  Change  DoGp(info) to 'await  DoGp(info)' and make the btnGo_Click an async Sub.  Once you 'await' the function you call you will also get the exception from the called function.

2) Since you just started with Pro Add-ins, i would suggest to make the leap to c# instead of using VB.  There are all kinds of advantages in using c# over vb.net, for example: VS has much better intellisense support for c#, most add-in samples and all snippets are in c# enabling copy/paste inheritance, this forum supports only a c# syntax highlighter for code snippets, vb is not supported.

0 Kudos
AbelPerez
Occasional Contributor II

Wolfgang Kaiser‌ thank you for the tips. I did look at several of you and Uma on starting an add-in. I viewed the one on multi threading and things are starting to make more sense. In response to your items:

1. After watching your video and you showing me what needs to be done it now works as it should. thanks!

2. I can code in c# but am not proficient. Over the next year or two I think I will make the transition. That being said, Microsoft are still supporting VB up to and including .NET Core 5 right? There are many developers still writing in VB as it has been our language for some time and I can produce code quite fast. One thing I do think should happen is that the documentation should be updated. For example ProConcepts Geoprocessing · Esri/arcgis-pro-sdk Wiki · GitHub  says in the header "Language: C# and Visual Basic". If no VB samples are provided then the header should not state it. Or it should say something like "Language: C#  (translate to VB here) with a link to Telerik Converter or some other online translator.

many thanks for all your help.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Abel,

 Microsoft will continue to support VB and so will the SDK so no worries there.  Your point about the documentation is correct and we will update the documentation for the 2.6 release this coming summer.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Abel,

 We updated the documentation for the 2.6 release which is coming out this week.

0 Kudos