Need help with error handling

I will do my best to try an explain my situation. I have a VERY large script. The main body of the script loads a single module with all the functions used in the script. The main body opens a windows form by calling a function (lets call this function A) in the module.

On the windows form, there is a Button. This button calls a function (lets call this function B) in the module. This function in turn calls another function (lets call this function C) that brings up a browse for folder dialog to the user. If the user cancels out of the browse for folder, I use a message box to ask the user if they want to exit, the browse for folder, if they select Yes, I exit function B and log that event and it is being correctly logged to my log file. This generates an error and I have no idea what function or better yet, what line number the error is coming from.

The error message is a Windows dialog: “You cannot call a method on a null-valued expression”. I can either hit “Details” and get the call stack, or I can hit “Continue” and carry on with my life. The title in the error dialog is the Title defined in my Windows Form so that tells me the function that loads the form (function B) is likely the culprit.

Is there any way to get more detail on where in my code the error is coming from as in a line number or function name? There is no way for me to post my code, just too much of it to post.

The call Stack gives me no clues, at last none that mean anything to me. Here is the call Stack:

System.Management.Automation.RuntimeException: You cannot call a method on a null-valued expression.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)
   at System.Management.Automation.ScriptBlock.<>c__DisplayClass57_0.<InvokeWithPipe>b__0()
   at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action)
   at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
   at System.Management.Automation.ScriptBlock.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object[] args)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Thanks in advance for any help you can provide.

Just a guess…
I suspect your script is continuing to run after the file dialog is closed and it’s whatever is expecting the input from the file dialog that’s throwing the error.

Thanks Matt. I never did figure out a way to get more detail on the error. Tried to figure out how to get JIT debugging in VS Code, no joy there. So, I simplified the code and that solved my problem.

Sure wish I could have figured out how to get more error info.

Is the error added to the $Error variable? You’d have to be in the same session but $Error[0] would be the last error object. The $Error array is reverse chronological so $Error[1] would be the one prior and so on.

The problem is the error dialog was a windows form that I had no control over. My options were “Error Details” which gave me the call stack, or a Continue button which went right back to the Windows form that generated the error which continued to function normally. The console that launched the script had no indication of any error, all I got was the windows form error dialog.

I was able to bird dog it down to a single “if” block and simply removed that block to fix the problem. At the end of the call stack were some instructions on enabling JIT debugging, but I was never able to get that working in VS Code.

I am going to leave as is. Thanks for your input :slight_smile: