xp-framework/rfc — Issue #181: SystemExit
Author: thekid
Created: 2011-10-17 16:03:29+00:00
State: closed
Labels: framework, implemented

Scope of Change
====
Calls to `exit()` will be replaced by a method that throws a
`lang.SystemExit` exception.

Rationale
====
Be able to catch sourcecode that tries to exit.

Functionality
====
Take into consideration the following:

```php
err->writeLine('*** ', $message);
exit(1);
}

public function run() {
// ...

if (!...) {
$this->bail('Could not ...');
}

// ...
}
}
?>
```

The problem here is this code was run inside, say, a GUI to show all
available Command classes and execute them by clicking on buttons,
the entire process would exit instead. This is of course not intended.

Instead, we simply want a simple means to exit from the `run()`
method.

Changes to code
----
So this is what has to change in the Command class:

```
- exit(1);
+ System::doExit(1);

```

On the calling side, we can now do the following:

```php
run(new ParamString(array_merge(array($class), $args)));
} catch (SystemExit $e) {
// ...
}
?>
```

Security considerations
====
None.

Speed impact
====
Exiting will be slightly slower due to the overhead throwing an exception
adds.

Dependencies
====
None.

Related documents
====
http://news.xp-framework.net/article/94/0000/00/00/