Howto:Add new fgcommands to FlightGear: Difference between revisions

m
Line 137: Line 137:
== Exception handling ==
== Exception handling ==


To be on the safe side, complex fgcommands should make use of exception handling to catch exceptions and deal with them gracefully, so that they cannot affect the simulator critically.
To be on the safe side, complex fgcommands should make use of exception handling to catch exceptions and deal with them gracefully, so that they cannot affect the simulator critically.  
 
This is accomplished by using standard C++ try/catch blocks, usually catching an sg_exception. The SG_LOG() macro can be used to print debugging information to the console.
 
For example, see the callback function "do_preferences_load" in [http://gitorious.org/fg/flightgear/blobs/next/src/Main/fg_commands.cxx#line445 line 445]:
 
<syntaxhighlight lang="cpp">
/**
* Built-in command: (re)load preferences.
*
* path (optional): the file name to load the panel from (relative
* to FG_ROOT). Defaults to "preferences.xml".
*/
static bool
do_preferences_load (const SGPropertyNode * arg)
{
  try {
    fgLoadProps(arg->getStringValue("path", "preferences.xml"),
                globals->get_props());
  } catch (const sg_exception &e) {
    guiErrorMessage("Error reading global preferences: ", e);
    return false;
  }
  SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
  return true;
}
</syntaxhighlight>


= Adding new commands =
= Adding new commands =