QlikView can work in three error modes. We will talk briefly about all of them, but focus will be put on Error Mode 0, which really gives us the control.
Error mode 1
We are all familiar with default mode (1). Below variable is automatically set by QlikView system during initial phase of script execution:
We’ve all seen (too many times!) the result of error handling by this mode – below window shows up when error occurs in script:
So Error Mode 1 pauses script execution and asks user for action. We can either click OK and move to next statement or click Cancel and exit from application.
Error mode 2
How can we change the default behaviour? Simply by overwriting system variable ErrorMode.
Mode 2 will close QlikView document immediately when error is encountered. No additional prompts will be shown:
Error mode 0
The most interesting Error Mode is 0. In this mode QlikView omits errors. When one occurs, script simply starts executing next statement. This mode gives us real control on error handling. First of all, when error occurs its code is saved to ScriptError variable.
List of all codes:
|3||General ODBC error|
|4||General OLE DB error|
|5||General custom database error|
|6||General XML error|
|7||General HTML error|
|8||File not found|
|9||Database not found|
|10||Table not found|
|11||Field not found|
|12||File has wrong format|
|14||BIFF error encrypted|
|15||BIFF error unsupported version|
Knowing above, we can implement something like try..catch clause in our script. For example, if script does not find the file to load, instead of stopping execution we can try loading file from other location. Example:
SET ErrorMode = 0; LOAD * FROM [Transaction.qvd] (qvd); if ScriptError=8 then LOAD * FROM [\Other Folder\Transaction.qvd] (qvd); if ScriptError=8 then exit script; end if end if
Above will try loading Transactions.qvd from QlikView document folder. If failed, it will try loading it from “Other Folder”. If file is not found there as well, “Exit script” statement will be executed.
We can mix various Error Modes together. If we prefer script to fail, when other file is not found we can simply do that:
SET ErrorMode = 0; LOAD * FROM [Transaction.qvd] (qvd); if ScriptError=8 then SET ErrorMode = 2; LOAD * FROM [\Other Folder\Transaction.qvd] (qvd); end if
Now, when file is not found in second location, script will (as per ErrorMode 2) close QlikView document immediately.
We have 2 more error handling variables available:
- ScriptErrorCount – returns number of errors, which occurred during script execution.
- ScriptErrorList – returns concatenated list of all error names.
We can use ScriptErrorCount to trigger an Alert after reload, informing administrators about exact errors stored in ScriptErrorList.