{"name":"catch","title":"catch","type":"command","url":"https://community.bistudio.com/wiki/catch","revid":369419,"updated":"2024-09-03T18:48:49Z","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":"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","params":[{"name":"tryBlock","type":"Exception Type","type_expr":"Exception handling"},{"name":"code","type":"Code","type_expr":"Code"}],"returns":{"type":"Anything","type_expr":"Anything"},"form":"binary","left":{"text":"tryBlock"},"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":"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};"]}],"see_also":["Exception handling","try","throw"]}