Exception handling: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(completed)
m (Some wiki formatting)
 
(21 intermediate revisions by 11 users not shown)
Line 1: Line 1:
[[Category:Armed Assault: Scripting]]
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.


In Armed Assault is implemented system of [[Armde Assault:Scripting commands|scripting commands]] which allows your scripts react to lots of kinds of exceptions.
Standard construction is:
Standard construction is:
<sqf>
try
{
// code block that can throw exception
if (_name == "") then
{
throw "no name";
}
else
{
titleText [format ["Good morning, Captain %1.", _name], "PLAIN DOWN"];
sleep 1;
titleText [_name, "PLAIN DOWN"];
};
}
catch
{
// code block that processes an exception
if (_exception == "no name") then
{
hint "Name was not entered";
titleText ["And the name isn't", "PLAIN DOWN"];
};
};
</sqf>


try {
{{Feature|important|{{Name|arma}} 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 used for error trapping).
    //block, that can throw exception
    if (_name == "") then {
        throw "invalid _name"
    } else {
        TitleText [format["Good morning, Captain %1.", _name], "PLAIN DOWN"]
        ~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"]
    }
}


The following would therefore '''not''' create a catchable exception:
<sqf>
try
{
a = 1 / 0; // SQF error happens here already
}
catch
{
hint "illegal operation"; // useless
};
</sqf>
}}


{{AnswerMe}}
Probably is this possible too:


{{Box File|.../fireBomb.sqs|<pre>
[[Category: Scripting Topics]]
_car = _this select 0
if (_car emptyPositions "driver") || _car emptyPositions "gunner" || _car emptyPositions "commander" ) then {
    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 {
    TitleText ["Sgt. Detritus: I get bomb to his car ;-)", "PLAIN DOWN"]
    [jeepOne] exec "fireBomb.sqs"
}<br>
catch {
    if (_exception == "vehicle empty") then {
        TitleText ["Sgt. Detritus: He have luck, but next time I'll kill him!", "PLAIN DOWN"]
    } else {
        TitleText [format["Sgt. Detritus: Some strange error appears... %1... hmm... another time I'll get him!", _exception], "PLAIN DOWN"]
}

Latest revision as of 00:14, 22 July 2022

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.

Standard construction is:

try { // code block that can throw exception if (_name == "") then { throw "no name"; } else { titleText [format ["Good morning, Captain %1.", _name], "PLAIN DOWN"]; sleep 1; titleText [_name, "PLAIN DOWN"]; }; } catch { // code block that processes an exception if (_exception == "no name") then { hint "Name was not entered"; titleText ["And the name isn't", "PLAIN DOWN"]; }; };

Arma 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 used for error trapping).

The following would therefore not create a catchable exception:

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