Exception handling: Difference between revisions

From Bohemia Interactive Community
m (Some wiki formatting)
(Some wiki formatting, Add alt syntax, Add Introduced with category)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
In {{arma1}} exception handling is an implemented system of special scripting commands ([[try]], [[catch]] and [[throw]]), which allows your scripts to create and to react to exceptions.
{{GVI|arma1|1.00}}


Standard construction is:
{{arma1}} introduced exception handling through the [[try]], [[catch]] and [[throw]] scripting commands, allowing scripts to create and react to exceptions.


[[try]]
Standard structure:
{
<sqf>
{{cc|code block that can throw exception}}
try // code block that can throw exception
[[if]] (_name == "") [[then]]
{
{
if (_name == "") then { throw "no name"; }; // this exits the [[try]] block and enters the [[catch]] block below
[[throw]] "no name"
if (_name == "") throw "no name"; // since {{arma3}} v1.54
}
[[else]]
{
[[titleText]] [[[format]] ["Good morning, Captain %1.", _name], "PLAIN DOWN"]
~1
[[titleText]] [_name, "PLAIN DOWN"]
}
}
[[catch]]
{
{{cc|code block that processes an exception}}
[[if]] (_exception == "no name") [[then]]
{
[[hint]] "Name was not entered"
[[titleText]] ["And the name isn't", "PLAIN DOWN"]
}
};


{{Feature|important|{{Name|arma1|short}} scripting commands do '''not''' create SQF exceptions by themselves if they encounter an illegal situation, they throw a compilation exception (i.e. the here-described exception handling cannot be usedfor error trapping).
titleText [format ["Good morning, Captain %1.", _name], "PLAIN DOWN"];
}
catch // code block that processes an exception
{
if (_exception == "no name") then
{
hint "Name was not entered";
titleText ["Good morning, anonymous Captain.", "PLAIN DOWN"];
};
};
</sqf>
 
 
{{Feature|important|
{{Name|rv}} scripting commands do '''not''' create SQF exceptions by themselves if they encounter an illegal situation,
they throw a '''''compilation''''' exception that cannot be caught by this try-catch structure explained here.


The following would therefore '''not''' create a catchable exception:
The following would therefore '''not''' create a catchable exception:
<code>[[try]]<br>{<br> a {{=}} 1/0; {{cc|SQF error happens here already}}<br>}<br>[[catch]]<br>{<br> [[hint]] "illegal operation";<br>};</code>
<sqf>
_divider = 0;
try { a = 1 / _divider; } // SQF error happens here already
catch { hint "illegal operation"; }; // useless
</sqf>
 
The proper way:
<sqf>
_divider = 0;
try { if (_divider == 0) then { throw "div0"; }; a = 1 / _divider; }
catch { hint "illegal operation"; };
</sqf>
}}
}}




[[Category: Scripting Topics]]
[[Category: Scripting Topics]]
[[Category:Introduced with Armed Assault version 1.00]]

Latest revision as of 18:15, 7 January 2026

Logo A1 black.png 1.00

Armed Assault introduced exception handling through the try, catch and throw scripting commands, allowing scripts to create and react to exceptions.

Standard structure:

try // code block that can throw exception { if (_name == "") then { throw "no name"; }; // this exits the try block and enters the catch block below if (_name == "") throw "no name"; // since Arma 3 v1.54 titleText [format ["Good morning, Captain %1.", _name], "PLAIN DOWN"]; } catch // code block that processes an exception { if (_exception == "no name") then { hint "Name was not entered"; titleText ["Good morning, anonymous Captain.", "PLAIN DOWN"]; }; };


Real Virtuality scripting commands do not create SQF exceptions by themselves if they encounter an illegal situation,

they throw a compilation exception that cannot be caught by this try-catch structure explained here.

The following would therefore not create a catchable exception:

_divider = 0; try { a = 1 / _divider; } // SQF error happens here already catch { hint "illegal operation"; }; // useless

The proper way:

_divider = 0; try { if (_divider == 0) then { throw "div0"; }; a = 1 / _divider; } catch { hint "illegal operation"; };