Exception handling: Difference between revisions

From Bohemia Interactive Community
m (changed introduction text)
(Some wiki formatting, Add alt syntax, Add Introduced with category)
 
(19 intermediate revisions by 10 users not shown)
Line 1: Line 1:
[[Category:Armed Assault: Editing]]
{{GVI|arma1|1.00}}


In Armed Assault 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.
{{arma1}} introduced exception handling through the [[try]], [[catch]] and [[throw]] scripting commands, allowing scripts to create and react to exceptions.
Standard construction is:


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


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>


{{AnswerMe}}
Probably is this possible too:


{{Box File|.../fireBomb.sqs|<pre>
{{Feature|important|
_car = _this select 0
{{Name|rv}} scripting commands do '''not''' create SQF exceptions by themselves if they encounter an illegal situation,
if (crew _car == 0) then {
they throw a '''''compilation''''' exception that cannot be caught by this try-catch structure explained here.
    throw "vehicle empty"
} else {
    if (3 < random 10) then {
        throw "bomb failed"
    } else {
        _car setDammage 1
        if (alive Guba) then {
            throw "bastard still alive"
        }
    }
}     
</pre>}}


try {
The following would therefore '''not''' create a catchable exception:
    TitleText ["Sgt. Detritus: I get bomb to his car ;-)", "PLAIN DOWN"]
<sqf>
    [jeepOne] exec "fireBomb.sqs"
_divider = 0;
}<br>
try { a = 1 / _divider; } // SQF error happens here already
catch {
catch { hint "illegal operation"; }; // useless
    if (_exception == "vehicle empty") then {
</sqf>
        TitleText ["Sgt. Detritus: He have luck, but next time I'll kill him!", "PLAIN DOWN"]
 
    } else {
The proper way:
        TitleText [format["Sgt. Detritus: Some strange error appears... %1... hmm... another time I'll get him!", _exception], "PLAIN DOWN"]
<sqf>
}
_divider = 0;
try { if (_divider == 0) then { throw "div0"; }; a = 1 / _divider; }
catch { hint "illegal operation"; };
</sqf>
}}
 
 
[[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"; };