Handle run-time errors

Syntax1 - on error, continue

err[+|-]

 

Syntax2 - on error, execute code and continue

err[+|-]
(tab)statements
(tab)...

 

Can be in single line:

err[+|-] statements

 

Parameters

Options:

Default handle errors only from the preceding statement.
+ handle errors from all preceding statements (or from all preceding statements that follow err-).
- marks the beginning of the block of statements guarded by err+.

 

Remarks

Use err to continue macro when a run-time error occurs.

 

When an error occurs, QM looks for err in the next statement, or for err+ in some statement forward. If finds, execution continues after that err or err+. If not, macro ends.

 

Syntax2: On error executes statements. If no error, skips statements.

 

err handles errors generated by QM functions, user-defined functions (see end) and COM functions. Also it handles exceptions (errors generated by the OS). It does not handle some fatal errors such as "noncompiled function". It does not handle compile-time errors.

 

When an error occurs, special variable _error is filled with information about the error. Type:

 

type QMERROR ~description code iid source place ~line

 

description - string that you can see in QM output when error is generated.

code - associated numeric value.

iid - id of QM item where the error is generated. Use str.getmacro to get item name. Use getopt to get macro entry.

source - one of the following values: 0 - no error, 1 - compile-time error, 2 - run-time error generated by QM, 3 - run-time error generated by end, 4 - exception (run-time error generated by the operating system).

place - offset of the statement in macro text.

line - the statement.

 

_error has thread scope, that is, is accessible in all functions of that thread, including functions registered to run when thread ends (atend). In function registered by atend, _error is empty if the thread ended normally, or is populated with the error info if the thread ended due to an error.

 

err handles errors generated in current macro/function only. It does not handle errors generated in user-defined functions that are called from current macro/function. However, called functions can pass their errors to the caller. To pass errors generated in current function to the caller function, handle them with err+ at the end, and use end _error in the error handler code. It is like catch(...){throw;} in C++. See the example.

 

To see possible errors, check menu Run -> Show RT Errors. Then, when you compile a macro (click Run or Compile button), it shows commands/functions that may throw errors when the macro runs. It adds indicators in code editor. If you Alt+click an indicator, it shows the errors in output. This feature helps you to decide where to use err. Does not show some errors.

 

See also: Common errors, opt err, error constants

 

Examples

 Close "Notepad" window; ignore possible error (on error don't end macro):
clo "Notepad"; err

 Activate "Notepad" window; on error display error description:
act "Notepad"
err
	out _error.description

 Handle errors from all preceding statements and generate them in caller:
err+ end _error

__________________________________

 Loop example:

out
run "notepad"
1
int i
for i 0 3
	out "activate Notepad"
	act "Notepad" ;;throws error if Notepad not found
	err ;;handles the error
		out _error.description
		continue
	out "close Notepad"
	clo "Notepad"

__________________________________

 If macro ends due to an error, log the error to file 'My Documents\My QM\qm log.txt':

 Insert this at the beginning of each macro that needs error logging:
atend LogErrors

 And create function LogErrors:
if(_error.source) ;;macro ended due to an error
	str macroname.getmacro(getopt(itemid 3) 1)
	str functionname.getmacro(_error.iid 1)
	str s.format("Macro %s ended due to an error in %s.[]Error description: %s[]Error line: %s[]" macroname functionname _error.description _error.line)
	LogFile s 1