{"name":"try","operator":null,"title":"try","type":"command","url":"https://community.bistudio.com/wiki/try","revid":369368,"updated":"2024-09-03T18:43:09Z","groups":["Program Flow"],"status":"ok","replaced_by":[],"games":[{"game":"arma1","name":"Armed Assault","version":"1.00"},{"game":"arma2","name":"Arma 2","version":"1.00"},{"game":"arma2oa","name":"Arma 2: Operation Arrowhead","version":"1.50"},{"game":"tkoh","name":"Take On Helicopters","version":"1.00"},{"game":"arma3","name":"Arma 3","version":"0.50"}],"since":{"game":"arma1","name":"Armed Assault","version":"1.00"},"arma3_since":"0.50","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"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","since":null,"effect":null,"exec":null,"params":[{"name":"code","type":"Code","optional":false,"default":null,"description":null,"types":["Code"],"since":null,"text":"code: Code"}],"returns":{"type":"Exception Type","types":["Exception handling"],"description":null,"text":"Exception Type"},"form":"unary","operator":null,"left":null,"right":{"text":"code","types":["Code"]}},{"syntax":"args try code","since":"arma3 1.54","effect":null,"exec":null,"params":[{"name":"args","type":"Anything","optional":false,"default":null,"description":"passed arguments, will be put in *_this* variable inside the \"code\"","types":["Anything"],"since":null,"text":"args: Anything - passed arguments, will be put in *_this* variable inside the \"code\""},{"name":"code","type":"Code","optional":false,"default":null,"description":null,"types":["Code"],"since":null,"text":"code: Code"}],"returns":{"type":"Exception Type","types":["Exception handling"],"description":null,"text":"Exception Type"},"form":"binary","operator":null,"left":{"text":"args","types":["Anything"]},"right":{"text":"code","types":["Code"]}}],"examples":[{"text":"```sqf\ntry { throw \"invalid argument\" } catch { hint str _exception };\n```","code":["try { throw \"invalid argument\" } catch { hint str _exception };"]},{"text":"```sqf\n123 try { if (_this != 123) throw \"invalid argument\" } catch { hint str _exception };\n```","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};"]}],"multiplayer":null,"problems":null,"aliases":[],"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."}],"categories":["ArmA: Armed Assault: New Scripting Commands","ArmA: Armed Assault: Scripting Commands","Arma 2: Operation Arrowhead: Scripting Commands","Arma 2: Scripting Commands","Arma 3: Scripting Commands","Command Group: Program Flow","Introduced with Armed Assault version 1.00","Scripting Commands","Take On Helicopters: Scripting Commands"]}