Debug Checkpoints

I have one long function and there’s a NilObjectException occurring on a customers machine in that function, I can get which function from the exception.

However I’m trying to devise a robust means of capturing where in the function the error occurs. Currently I’m using a string and every few lines set that string to be the line number in the IDE. At the end of the method it sets the string to “” so that I don’t get this information if an error occurs elsewhere. The problem lies in that this function has many “return earlies”, leaving that string set to the last check point.

So I devised an object for holding onto the check point messages, the idea being that if an error occurs in a function that uses these objects, I’d be able to grab the info from them, but if there’s no error, it doesn’t matter when the function is exited as the object would be destroyed.

Only to find that by capturing the error in app.unhandledException, all the objects are already destroyed by the time we get there. So I get no checkpoint information.

Anyone, any thoughts?

a) Refactor your long method into several short methods.

b) I haven’t a clue what you mean with your explanation about check point messages. Don’t make stuff so complicated:

code line code line log 1 code line code line log 2

and so on. Usually, after 2 iterations I have a clue what went wrong.

A ‘debug’ mode.
(I use ‘click this control while holding shift’ to turn it on)

When in debug mode, append a log to a global array of strings.
That wont be dead even in an unhandled exception.
When the app closes, or in the unhandled exception, you can write the log to disc or copy to the clipboard.

[quote=402979:@Jeff Tullin]When in debug mode, append a log to a global array of strings.
That wont be dead even in an unhandled exception.[/quote]
This would work, but I’d have to reset it either at the end of each function, or when it returns early, or at the start of each function.

For example

checkPoint = "1" doSomethings if thangFailed then return nil // --- Returns early as there's no point in continuing. checkPoint = "7" doSomeMoreThings if thingINeed = nil then return nil // --- Returns early as there's no point in continuing. checkPoint = "13" doABunchMoreStuff if thatSomething = smellsLikeFish then return nil // --- Returns early as there's no point in continuing. // --- All finished. checkPoint = "" return result

I am basically trying to implement a fit and forget solution, one that doesn’t require a lot of code and checking to make sure it’s reporting the correct checkpoint. What I want to be sure of, is that when the method is exited the checkPoint is cleared. Sure I could go through every opportunity to exit early and make sure that it’s reset there, but I can’t be sure that I won’t miss one.

Using system.debugLog, I have confirmed that the destructor is called on my checkPoint objects before the “App.UnhandledException” fires.

Which pretty much null and voids my design :frowning:

Then maybe you could call the function that sets the checkpoint from another function
For instance

CallMyTodoThings //This is a call to your function that contains the above code checkpoint="" //Xojo will never get here if there is an exception, therefore leaving the current status set, but it will reset the value when all is fine.
Is that an option?