Exception handling: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "wasn't" to "was not")
m (Some wiki formatting)
Line 1: Line 1:
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.
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.


Standard construction is:
Standard construction is:


  try {
  [[try]]
    //block, that can throw exception
{
    if (_name == "") then {
{{cc|code block that can throw exception}}
        throw "no name"
[[if]] (_name == "") [[then]]
    } else {
{
        TitleText [format["Good morning, Captain %1.", _name], "PLAIN DOWN"]
[[throw]] "no name"
        ~1
}
        TitleText [_name, "PLAIN DOWN"]
[[else]]
    }
{
  }<br>
[[titleText]] [[[format]] ["Good morning, Captain %1.", _name], "PLAIN DOWN"]
  catch {
~1
    //block, that processes an exception
[[titleText]] [_name, "PLAIN DOWN"]
    if (_exception == "no name") then {
}
        echo "Name was not entred"
  }
        TitleText ["And the name isn't", "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"]
}
  };
  };


'''Note:''' ArmA scripting commands do '''not''' create an exception by themselves if they encounter an illegal situation (i.e. you can't use the here described exception handling for error trapping.)
{{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).


The following would therefore '''not''' create a catchable exception:  
The following would therefore '''not''' create a catchable exception:
<code>try {a=1/0;} catch {hint "illegal operation";};</code>
<code>[[try]]<br>{<br> a {{=}} 1/0; {{cc|SQF error happens here already}}<br>}<br>[[catch]]<br>{<br> [[hint]] "illegal operation";<br>};</code>
}}




[[Category: Scripting Topics|Exception Handling]]
[[Category: Scripting Topics]]

Revision as of 01:59, 30 October 2021

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"]
		~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 usedfor error trapping).

The following would therefore not create a catchable exception:

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