try: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - " \{\{GameCategory\|[a-z]+[0-9]?\|Scripting Commands\}\}" to "")
m (Fix SQF)
 
(40 intermediate revisions by the same user not shown)
Line 19: Line 19:


|descr= Defines a try-catch structure. This sets up an [[Exception handling|exception handling]] block. Any thrown exception in a try block is caught in a [[catch]] block. The structured exception block has the following form:
|descr= Defines a try-catch structure. This sets up an [[Exception handling|exception handling]] block. Any thrown exception in a try block is caught in a [[catch]] block. The structured exception block has the following form:
<code>[[try]]
<sqf>try
{ {{codecomment|/* block that can throw exception */}} }
{ /* block that can throw exception */ }
[[catch]]
catch
{ {{codecomment|/* block that processes the exception. Exception is described in the ''_exception'' variable */}} };</code>
{ /* block that processes the exception. Exception is described in the _exception variable */ };</sqf>


|s1= [[try]] code
|s1= [[try]] code
Line 30: Line 30:
|r1= [[Exception handling|Exception Type]]
|r1= [[Exception handling|Exception Type]]


|s2= args [[try]] code {{Since|arma3|1.53.133045|y}}
|s2= args [[try]] code
 
|s2since= arma3 1.54


|p21= args: [[Anything]] - passed arguments, will be put in ''_this'' variable inside the "code"
|p21= args: [[Anything]] - passed arguments, will be put in ''_this'' variable inside the "code"
Line 38: Line 40:
|r2= [[Exception handling|Exception Type]]
|r2= [[Exception handling|Exception Type]]


|x1= <code>[[try]] {[[throw]] "invalid argument"} [[catch]] {[[hint]] [[str]] _exception};</code>
|x1= <sqf>try { throw "invalid argument" } catch { hint str _exception };</sqf>


|x2= {{Since|arma3|1.53.133045}}: <code>123 [[try]] {[[if]] (_this != 123) [[throw]] "invalid argument"} [[catch]] {[[hint]] [[str]] _exception};</code>
|x2= <sqf>123 try { if (_this != 123) throw "invalid argument" } catch { hint str _exception };</sqf>


|x3= The correct usage of shorthand alt syntax: <code>[[try]] {
|x3= <sqf>
[[if]] (a > b) [[throw]] "Error: some error"; {{cc|OK}}
try {
{{cc|The command argument is static}}
if (a > b) throw "Error: some error"; // OK
} [[catch]] {
// the command argument is static
[[hint]] [[str]] _exception;
} catch {
hint str _exception;
};
};


[[try]] {
try {
_someFunc = {
_someFunc = {
.....
// ...
};
};
[[if]] (a > b) [[throw]] ([[call]] _someFunc); {{cc|NOT OK}}
if (a > b) throw (call _someFunc); // NOT OK
{{cc|The command argument is dynamic}}
// the command argument is dynamic
{{cc|_someFunc is called first to get the value regardless of (a > b) outcome}}
// _someFunc is called first to get the value regardless of (a > b) outcome
} [[catch]] {
} catch {
[[hint]] [[str]] _exception;
hint str _exception;
};
};


[[try]] {
try {
_someFunc = {
_someFunc = {
.....
// ...
};
};
[[if]] (a > b) [[then]] {[[throw]] ([[call]] _someFunc)}; /// OK
if (a > b) then {throw (call _someFunc)}; // OK
{{cc|The command argument is dynamic}}
// the command argument is dynamic
{{cc|_someFunc is only called when (a > b) is [[true]]}}
// _someFunc is only called when (a > b) is true
} [[catch]] {
} catch {
[[hint]] [[str]] _exception;
hint str _exception;
};</code>
};
</sqf>


|seealso= [[Exception handling]], [[throw]], [[catch]]
|seealso= [[Exception handling]] [[throw]] [[catch]]
}}
}}


 
{{Note
<dl class="command_description">
|user= Killzone_Kid
 
|timestamp= 20150609205200
<dt></dt>
|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.
<dd class="notedate">Posted on June 9, 2015 - 20:52 (UTC)</dd>
}}
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
<dd class="note">
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.  
</dd>
 
</dl>

Latest revision as of 17:49, 1 November 2022

Hover & click on the images for description

Description

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:
try { /* block that can throw exception */ } catch { /* block that processes the exception. Exception is described in the _exception variable */ };
Groups:
Program Flow

Syntax

Syntax:
try code
Parameters:
code: Code
Return Value:
Exception Type

Alternative Syntax

Syntax:
args try code
Parameters:
args: Anything - passed arguments, will be put in _this variable inside the "code"
code: Code
Return Value:
Exception Type

Examples

Example 1:
try { throw "invalid argument" } catch { hint str _exception };
Example 2:
123 try { if (_this != 123) throw "invalid argument" } catch { hint str _exception };
Example 3:
try { if (a > b) throw "Error: some error"; // OK // the command argument is static } catch { hint str _exception; }; try { _someFunc = { // ... }; if (a > b) throw (call _someFunc); // NOT OK // the command argument is dynamic // _someFunc is called first to get the value regardless of (a > b) outcome } catch { hint str _exception; }; try { _someFunc = { // ... }; if (a > b) then {throw (call _someFunc)}; // OK // the command argument is dynamic // _someFunc is only called when (a > b) is true } catch { hint str _exception; };

Additional Information

See also:
Exception handling throw catch

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
Killzone_Kid - c
Posted on Jun 09, 2015 - 20:52 (UTC)
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.