Finding Errors easily using Try&Catch

Put simple Tips and Tricks that are not entire Tutorials in this forum
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Finding Errors easily using Try&Catch

Post by nnnik » 31 May 2018, 02:06

In AHK v1 the Errorlevel is set when an error occurs in any kind of function.
However you need to know where to look when finding which command changed the errorlevel - or if it is even a command that doesn't change the ErrorLevel.
This is quite the opposite of newbie friendly. As a newbie I remember getting stuck by this more times than I can imagine.

It would be so much easier if AHK could just tell you when an error happened - I mean it knows that an error happened why doesn't it simply tell you?
And AHK actually can tell you. All you have to do is wrap the code you should tell you when and where an error appears in this:

Code: Select all

Try {
	;your code here
} Catch e
	Throw e
One example of how it might look like:

Code: Select all

Try {
	Msgbox looking for things
	ImageSearch, outputX, outputY, 10, 10, 100, 100, missing.jpg
	Msgbox doing more things
} Catch e
	Throw e
You can also make newbies use this to quickly debug their script.
Recommends AHK Studio

icuurd12b42
Posts: 202
Joined: 14 Aug 2016, 04:08

Re: Finding Errors easily using Try&Catch

Post by icuurd12b42 » 31 May 2018, 02:30

You can also create your own exception
throw Exception ("This Happened In MyOpenFile() Trying to open file: " . filename . "--" . e.message,-1)

https://autohotkey.com/docs/commands/Th ... #Exception

User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Finding Errors easily using Try&Catch

Post by nnnik » 31 May 2018, 03:11

Yeah I guess thats also possible but defeats the point of finding this technique - which is better debugging not creating something that can be more easily debugged.
Though you can also use my tip for that but it's not the main focus of this tip.
Recommends AHK Studio

icuurd12b42
Posts: 202
Joined: 14 Aug 2016, 04:08

Re: Finding Errors easily using Try&Catch

Post by icuurd12b42 » 31 May 2018, 03:25

Well, as you say, knowing when the error happen... this implies debugging, it is quite possible to get the same exception from numerous locations in your code, the extra bit helped me when compiled code has an error.

Anyway, just putting it out there that you can supplement the exception (e)

Post Reply

Return to “Tips and Tricks (v1)”