{"name":"catch","operator":null,"title":"catch","type":"command","url":"https://community.bistudio.com/wiki/catch","revid":369419,"updated":"2024-09-03T18:48:49Z","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":"Processes code when an exception is thrown in a try block. The exception caught can be found in the *_exception* variable.","syntaxes":[{"syntax":"tryBlock catch code","since":null,"effect":null,"exec":null,"params":[{"name":"tryBlock","type":"Exception Type","optional":false,"default":null,"description":null,"types":["Exception handling"],"since":null,"text":"tryBlock: Exception Type"},{"name":"code","type":"Code","optional":false,"default":null,"description":null,"types":["Code"],"since":null,"text":"code: Code"}],"returns":{"type":"Anything","types":["Anything"],"description":null,"text":"Anything"},"form":"binary","operator":null,"left":{"text":"tryBlock","types":["Exception handling"]},"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":"The correct usage of shorthand alt syntax:\n\n```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","try","throw"],"notes":[],"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"]}