The Try command is used to "catch" errors that occur in a block of code. If an error occurs in a Try..EndTry code block, it doesn't display an error message and exit the program. Instead, it records the type of error in the system variable errornum, and jumps to the Else section of the Try..EndTry block.
Here, you have several options. By checking the Errors page, you can test errornum for specific values to find out what kind of error happened. Ultimately, you'll want to use one of two commands:
Here is an example of Try..EndTry in action:
:Try : UnArchiv var :Else : Disp "var is undefined!" : ClrErr :EndTry
For most errors, Try..EndTry blocks aren't the way to go because you want to prevent them in the first place. They are a good way to handle special cases or circumstances out of your control. Here are two situations you might use Try..EndTry in:
- If your program uses external variables, you might surround that part of the code in a Try..EndTry block, rather than do all the tests to make sure they are unlocked, unarchived, etc.
- If your program requires specially-formatted input, instead of sanity checking it first you might let the program take untreated input, and catch the errors that result from an incorrect format.
290 - EndTry is missing the matching Else statement happens when the Try..EndTry block doesn't contain an Else.
490 - Invalid in Try..EndTry block happens when ??.