How to display call stack by default? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

How to display call stack by default?

26 Sep 2021, 15:01

When an error occurs, it would be helpful for me to see the call stack, rather than just current line. I think I can make/register my own custom error handler using OnError. Does anyone have a good example from their own libraries of a handler that does this?

Is creating an Error handler the only way to modify the behavior? Is there a way to modify the Error object itself (or subclass it) in a way that causes the standard error dialog contents to include the stack?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to display call stack by default?  Topic is solved

26 Sep 2021, 17:39

Code: Select all

#Requires AutoHotkey v2.0-beta.1

OnError(handler)
handler(Err, Mode) {
	msg := 'Error: ' Err.Message '`n`n'

	if (Err.Extra != '')
		msg .= 'Specifically: ' Err.Extra '`n`n'

	msg .= '`tLine#`n'

	for line in StrSplit(Err.Stack, '`n')
	{
		; if can extract lineNr digits and source text?
		if RegExMatch(line, '^\Q' A_ScriptFullPath '\E \((\d+)\) (:.*)$', &M)
		{
			lineNr := Format('{:05}', M[1]) ; zero left pad 5x
			lineText := M[2] ; caller + src text

			if (A_Index = 1)
				msg .= '--->`t' lineNr lineText
			else
				msg .= '`t' lineNr lineText
		}
		else
			msg .= '`t' line ; eg '> Auto-Execute' or other unexpected formats

		msg .= '`n'
	}

	switch Mode
	{
	case 'Return':
		msg .= 'Try to continue anyway?'

		static MB_TOPMOST := 0x00040000, MB_DEFBUTTON2 := 0x00000100, MB_YESNO := 0x00000004
		switch MsgBox(msg, , MB_TOPMOST | MB_DEFBUTTON2 | MB_YESNO)
		{
		case 'Yes': return -1
		case 'No': return 0
		}
	case 'Exit': msg .= 'The current thread will exit.'
	case 'ExitApp': msg .= 'The program is now unstable and will exit.'
	}

	MsgBox msg
}
sirksel wrote:
26 Sep 2021, 15:01
Is there a way to modify the Error object itself (or subclass it) in a way that causes the standard error dialog contents to include the stack?
maybe but wouldnt recommend, since u cant get by the character limitations
sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

Re: How to display call stack by default?

26 Sep 2021, 17:51

@swagfag, perfect! Just what I was looking for. Thanks!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Pyper and 69 guests