Possible to detect if in try block?

Get help with using AutoHotkey and its commands and hotkeys
Sam_
Posts: 106
Joined: 20 Mar 2014, 20:24

Possible to detect if in try block?

27 Mar 2019, 12:20

Is it possible to detect if the current running code (let's say a function) is encapsulated within a try block?
User avatar
SALZKARTOFFEEEL
Posts: 72
Joined: 10 May 2017, 11:14
GitHub: SALZKARTOFFEEEL
Location: Germany
Contact:

Re: Possible to detect if in try block?

27 Mar 2019, 12:38

throw an exception and see what happens..?
Sam_
Posts: 106
Joined: 20 Mar 2014, 20:24

Re: Possible to detect if in try block?

27 Mar 2019, 12:58

I'm throwing an exception. If the exception is thrown in a try block, I want to include a traceback in the exception object. If it is not, I need to wait to add the traceback until the exception is caught by OnError().
User avatar
SALZKARTOFFEEEL
Posts: 72
Joined: 10 May 2017, 11:14
GitHub: SALZKARTOFFEEEL
Location: Germany
Contact:

Re: Possible to detect if in try block?

27 Mar 2019, 13:06

I don't really understand what you mean. The function specified by OnError() is called immediately when the exception is thrown. It happens at the exact same time as, say, the catch block would execute or the error message would be displayed.
swagfag
Posts: 2912
Joined: 11 Jan 2017, 17:59

Re: Possible to detect if in try block?

27 Mar 2019, 13:22

i think u need to post code man
none of this makes any sense tbh
Sam_
Posts: 106
Joined: 20 Mar 2014, 20:24

Re: Possible to detect if in try block?

27 Mar 2019, 16:20

I want every exception object to include a traceback of the current callstack. As far as I can tell, the situation is as follows:

If you are in a try block:
  • The only way to include a traceback is to add it to the exception object before it is thrown via throw (that is to say, before the stack unwinds and so before entering the catch block).
  • There is no way to add a traceback to an exception thrown by a runtime error.
If you are not in a try block but you have specified an OnError() function:
  • OnError is called before the stack unwinds, so a traceback can be added to every exception (including runtime errors and those created via throw) by the specified function.

Unless I’m misunderstanding something (which is probably likely) this leaves us with an issue. If we want to throw an exception with something like

Code: Select all

throw Exception("We threw an exception",,"Things went wrong...`n`n" Traceback())
where “Traceback()” returns a trace of the stack, something like

Code: Select all

Traceback (most recent call last):
  File "C:\Programs\AutoHotkey Scripts\Traceback\Handled.ahk", line 16 called Main()
  File "C:\Programs\AutoHotkey Scripts\Traceback\Handled.ahk", line 24 called f()
  File "C:\Programs\AutoHotkey Scripts\Traceback\Handled.ahk", line 49 called a()
  File "C:\Programs\AutoHotkey Scripts\Traceback\Handled.ahk", line 34 called b()
  File "C:\Programs\AutoHotkey Scripts\Traceback\Handled.ahk", line 38 called c()
  File "C:\Programs\AutoHotkey Scripts\Traceback\Handled.ahk", line 44 in c:
In such a situation, you would want to use

Code: Select all

throw Exception("We threw an exception",,"Things went wrong...`n`n" Traceback())
if you are in a try block and

Code: Select all

throw Exception("We threw an exception",,"Things went wrong...)
if you are not, because the OnError function will be adding the traceback already. Although I suppose since the answer to my original question seems to be “no”, I can instead try to make the OnError function guess whether the callstack has already been added, and not add it a 2nd time in that case. I’ll try that.
User avatar
SALZKARTOFFEEEL
Posts: 72
Joined: 10 May 2017, 11:14
GitHub: SALZKARTOFFEEEL
Location: Germany
Contact:

Re: Possible to detect if in try block?

27 Mar 2019, 18:23

I now understand your problem.
However, I think you are misunderstanding some things.

First off, “runtime errors” are errors that get thrown while the script runs. There is only one other type of error – the “on-load” error. Only runtime errors can be caught in any way.
Also, again, both OnError() and a try block work in the same way, they activate as soon as the exception is thrown – the internal stack does not unwind, nothing happens before the OnError() functions or catch get called.
Think about it, when an OnError() function returns a non-zero value, then the usual behavior continues – so the function must be called even before that usual behavior happens; even that usual behavior, however, is to stop execution instantly and show a message box.

I really don't think there is any real problem here. Using try and catch, and using OnError() should do the exact same thing, essentially.

Return to “Ask For Help”

Who is online

Users browsing this forum: Cerberus, DizzyH, flyingDman, gwarble, toralf, Xtra and 213 guests