Possible to detect if in try block?
Possible to detect if in try block?
Is it possible to detect if the current running code (let's say a function) is encapsulated within a try block?
- SALZKARTOFFEEEL
- Posts: 89
- Joined: 10 May 2017, 11:14
- Location: Germany
- Contact:
Re: Possible to detect if in try block?
throw an exception and see what happens..?
Re: Possible to detect if in try block?
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().
- SALZKARTOFFEEEL
- Posts: 89
- Joined: 10 May 2017, 11:14
- Location: Germany
- Contact:
Re: Possible to detect if in try block?
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.
Re: Possible to detect if in try block?
i think u need to post code man
none of this makes any sense tbh
none of this makes any sense tbh
Re: Possible to detect if in try block?
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:
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
where “Traceback()” returns a trace of the stack, something like
In such a situation, you would want to use
if you are in a try block and
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.
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.
- 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())
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:
Code: Select all
throw Exception("We threw an exception",,"Things went wrong...`n`n" Traceback())
Code: Select all
throw Exception("We threw an exception",,"Things went wrong...)
- SALZKARTOFFEEEL
- Posts: 89
- Joined: 10 May 2017, 11:14
- Location: Germany
- Contact:
Re: Possible to detect if in try block?
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.
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.