{"name":"try","title":"try","type":"command","url":"https://community.bistudio.com/wiki/try","revid":369368,"updated":"2024-09-03T18:43:09Z","groups":["Program Flow"],"status":"ok","games":{"arma1":"1.00","arma2":"1.00","arma2oa":"1.50","tkoh":"1.00","arma3":"0.50"},"since":"arma1 1.00","arma3_since":"0.50","description":"Defines a try-catch structure. This sets up an exception handling block. Any thrown exception in a try block is caught in a catch block. The structured exception block has the following form:\n\n```sqf\ntry\n{ /* block that can throw exception */ }\ncatch\n{ /* block that processes the exception. Exception is described in the _exception variable */ };\n```","syntaxes":[{"syntax":"try code","params":[{"name":"code","type":"Code","type_expr":"Code"}],"returns":{"type":"Exception Type","type_expr":"Exception handling"},"form":"unary","right":{"text":"code"}},{"syntax":"args try code","since":"arma3 1.54","params":[{"name":"args","type":"Anything","description":"passed arguments, will be put in *_this* variable inside the \"code\"","type_expr":"Anything"},{"name":"code","type":"Code","type_expr":"Code"}],"returns":{"type":"Exception Type","type_expr":"Exception handling"},"form":"binary","left":{"text":"args"},"right":{"text":"code"}}],"examples":[{"code":["try { throw \"invalid argument\" } catch { hint str _exception };"]},{"code":["123 try { if (_this != 123) throw \"invalid argument\" } catch { hint str _exception };"]},{"text":"```sqf\ntry {\n    if (a > b) throw \"Error: some error\"; // OK\n    // the command argument is static\n} catch {\n    hint str _exception;\n};\n\ntry {\n    _someFunc = {\n        // ...\n    };\n    if (a > b) throw (call _someFunc); // NOT OK\n    // the command argument is dynamic\n    // _someFunc is called first to get the value regardless of (a > b) outcome\n} catch {\n    hint str _exception;\n};\n\ntry {\n    _someFunc = {\n        // ...\n    };\n    if (a > b) then {throw (call _someFunc)}; // OK\n    // the command argument is dynamic\n    // _someFunc is only called when (a > b) is true\n} catch {\n    hint str _exception;\n};\n```","code":["try {\n\tif (a > b) throw \"Error: some error\"; // OK\n\t// the command argument is static\n} catch {\n\thint str _exception;\n};\n\ntry {\n\t_someFunc = {\n\t\t// ...\n\t};\n\tif (a > b) throw (call _someFunc); // NOT OK\n\t// the command argument is dynamic\n\t// _someFunc is called first to get the value regardless of (a > b) outcome\n} catch {\n\thint str _exception;\n};\n\ntry {\n\t_someFunc = {\n\t\t// ...\n\t};\n\tif (a > b) then {throw (call _someFunc)}; // OK\n\t// the command argument is dynamic\n\t// _someFunc is only called when (a > b) is true\n} catch {\n\thint str _exception;\n};"]}],"see_also":["Exception handling","throw","catch"],"notes":[{"user":"Killzone_Kid","date":"2015-06-09T20:52:00Z","text":"Do not expect this behave like Javascript try catch and ignore all errors. But it does have one useful behaviour. Normally when a runtime error occurs in SQF (unlike when there is compile error) it continues to execute till the end. But if the script is placed in try {} scope and throw is used upon error, the script immediately terminates, exits the try {} scope and enters catch {} scope. This way it is possible to process possible exceptions in civilised manner."}]}