I was working on my Excel Function library and ran into a snag. Maestrith helped me figure it out however I’m still not loving that I can’t trust “is Integer”. make sure you add something to your variable (even zero) to force it to be numeric!
When, Why and How to use Error Checking on Runtime Errors
Script Highlight: Detecting Carrier type, location, company from a phone number.
When / Why to use Error Checking
For general troubleshooting of your own code use:
Runtime Errors– An error that occurs during the execution of a program
- Use Error Handling to diagnose Runtime Errors for scripts distributed to clients
- Allows you to adapt the reported error as something meaningful to non-coders
- Frequently provides information you cannot get elsewhere
- Easy to Log the error with additional information so we can have context of what is frequently going wrong (Users are typically terrible are providing enough information)
- Can prevent the script from “locking up” / throwing an Error that is confusing to the client
- Build-into your Class / Library to help others utilizing your code
ErrorLevel and OnError
- Built-in variable which indicates success/failure of many (84) commands
- A value of Zero means success. Most commands return a 1 for failure but some have values higher than 1
- Specifies a function to run automatically when an unhandled error occurs.
- A Built-in Function name/Object to call when your script is exiting
- It can help understand why/when your script exited
There are 84 Built-in Command/Functions with Error Level
Try / Catch / Throw / Finally / Exception
- Try– Will “try” to execute a given line(s) of code
- Catch– Specifies the code to execute if an exception is raised during execution of a Try statement.
- Throw– Signals the existence of an error allowing a Try/Catch command to display info about this error. This object allows you to personalize the message / information available
- Finally– Used in conjunction with Try or Catch. Ensures that one or more statements (commands or expressions) are always executed after the execution of a try statement
- Exception()- An Object with properties to be accessed from Catch’s OutputVar
- Message:An error message or ErrorLevelvalue.
- What:The name of the command, function or label which was executing or about to execute when the error occurred.
- Extra:Additional information about the error, if available.
- File:Set automatically to the full path of the script file which contains the line at which the error occurred.
- Line:Set automatically to the line number at which the error occurred.
Best Practices / Tips & Tricks
- Remember –in Try / Catch the Exception thrown is an object.
- Not all programs accurately report ErrorLevel when run from RunWait
- –If this is the case, think of other ways to determine if your script finished correctly
- Unless you truly have a need, don’t worry about Throw or Finally. Most developers do not use them
- While typically used as pairs, Try can be used without Catch
- — in this case, it is used only to bypass the error message
- ComObjError()– can be set to 0 to disable COM errors (but isn’t recommended)
- Copy the ErrorLevel value to your own variable immediately after the command that caused the error (before its content is changed after executing another command)
This is a quick review of getting text from a page using Web Scraping with AutoHotkey. Often people new to Web Scraping will get hung-up on a minor detail wasting a lot of time. This video helps point out a few of the common issues like not having a valid pointer and returning the wrong attributes. Get the iWB2 Learner tool (remember it only works with IE. If you’re beyond this video you might also check out this one dealing with troubleshooting.
Below is the code I review in the video
Web Scraping with AutoHotkey video overview- Returning the correct content
Web Scraping with AutoHotKey: Troubleshooting Web Scraping
When building my first scraping scripts I used to waste a ton of time trying to figure out what was broken. Adding some structure to your diagnoses process can greatly speed-up detecting what has gone wrong. A copy of the AutoHotKey syntax writer can be found here.
I think some excellent advice, not exclusive to troubleshooting web scraping, is to have a bobble-doll or something to talk to. Pretend you’re explaining your issue to a friend and often, when you hear yourself say the words, your issue will appear to you.
This video offers some general troubleshooting tips around troubleshooting web scraping when using AutoHotKey.