is there a way of handling a .dlx file not found when "loading" a GUI
Im my Main () I have the following code:
Sub Main() theLW.Open() 'Display the GUI to get user's data MyMacro_GUI.Main() GetOnWithIt() End sub
and in the Public Class MyMacro_GUI the Sub New() function has the following
Public Sub New() Try theSession = Session.GetSession() theUI = UI.GetUI() theDialogName = IO.Path.ChangeExtension(theSession.ExecutingJourna
l, "dlx") If theDialogName = "" Then theDialogName = "MyMacro_GUI.dlx" Else If Not IO.File.Exists(theDialogName) Then theDialogName = "MyMacro_GUI.dlx" End If End If theDialog = theUI.CreateDialog(theDialogName) theDialog.AddApplyHandler(AddressOf apply_cb) theDialog.AddOkHandler(AddressOf ok_cb) theDialog.AddUpdateHandler(AddressOf update_cb) theDialog.AddInitializeHandler(AddressOf initialize_cb) theDialog.AddFocusNotifyHandler(AddressOf focusNotify_cb) theDialog.AddKeyboardFocusNotifyHandler(AddressOf keyboardFocusNotify_cb) theDialog.AddDialogShownHandler(AddressOf dialogShown_cb) Catch ex As Exception '---- Enter your exception handling code here ----- 'If macro arrives here it means that the dlx file has not been found theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString) Throw ex 'theLw.WriteLine("Error occurred while loading the dedicated GUI") 'theLw.WriteLine("The most likely cause is the .dlx file was not found") End Try End Sub
Is it possible to "return" something to the Main() function (ie the calling function) to handle the error. For example if one could return ex (the thrown error code) then I could "carry on" by going through serie in user input boxes (1 at a time!) to get the input (using NXInputbox). No pretty but it could work.
Any help would be very appreciated
In VB, a Sub does not return a value, but a Function can do that.
(In the Main)
Dim theTest As Exception = returnException()
If theTest IsNot Nothing Then
Echo("Exception: " & theTest.Message)
Instead of a Sub:
Function returnException() As Exception
'... do something in a try/catch
' From the Catch block, return the exception
In the Catch block, you will see the line:
This will pass the error "up one level" to the sub or function that called the currently executing code, in your case this means the error will show up in Sub Main(). You should wrap the call to MyMacro_GUI.New() in a try block to catch this error. You can then use the exception object's properties (such as .Message) to get more information on what error occurred.
Below are a few links that may help:
Thanks for the info SteveL & Cowski1. Much appreciacted
Does the Throw ex statment return to the Public Shared Sub Main() which is defined in Public Class MyMacro_GUI or does it return to Sub Main() defined in the Module? in the module
I have the following;
Module MyMacro Public Dim theSession As Session = Session.GetSession() Public Dim theUISession As UI = UI.GetUI Public Dim theLW As ListingWindow = theSession.ListingWindow Dim strMacroName as String = "MyMacro010" Public theNXMessageBox As NXMessageBox = theUISession.NXMessageBox Public theNXMessageBoxTitle As String = "My Macro" Public Dim theBasePart As BasePart = theSession.Parts.BaseWork Public Dim theSimPart As NXOpen.CAE.SimPart = theSession.Parts.BaseWork '-------------------------------- Sub Main() theLW.Open() theLW.WriteLine ("-----------------------------------") theLW.WriteLine ("Starting macro '" & strMacroName & "'...") If (theBasePart is Nothing Or Not (TypeOf TheBasePart Is CAE.SimPart)) Then Dim Msg as String = "Macro '" & strMacroName & " ' only works on a SIM part" Dim Title as String = strMacroName MsgBox(Msg, vbOKOnly + vbCritical, Title) theLW.WriteLine ("End of macro...") theLW.WriteLine ("-----------------------------------")
End if 'Display the GUI MyMacro_GUI.Main() GetOnWithIt() End sub
The exception object will be thrown to whatever sub or function called the MyMacro_GUI.New subroutine. Where is the MyMacro_GUI object created?
Thanks cowski1. I think I have my tought a bit muddle here (my limited vba knowledge is showing now!) I think I am doing the following;
Module MyMacro 'Lauch the GUI MyMacro_GUI.Main() TheProgramToBeExecuted() End Module '$------ Public Class MyMacro_GUI Public Shared Sub Main() Try theMyMacro_GUI = New MyMacro_GUI() ' The following method shows the dialog immediately theMyMacro_GUI.Show() Catch ex As Exception '---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString) Finally If theMyMacro_GUI IsNot Nothing Then theMyMacro010_GUI.Dispose() End If End Try End Sub Public Sun New() 'as defined in previous post End Sub End Class
My understanding is the MyMacro_GUI.Main() is called which in turns try creating a New (instance!) of the GUI when theMyMacro_GUI = New MyMacro_GUI() is used
So based on your comment the exception is thrown back to MyMacro_GUI.Main()
Yes, the error should show up in the MyMacro_GUI.Main method. Since your code is in a Try block at that point, it shold transfer control to the Catch block and you would see an NX messagebox pop up with the error description.
Is this what is happening? You should be able to re-thow the error up another level if that is what you want to do.