sys.exit ()
and ʻexitthrow a built-in exception
SystemExit. Therefore, it behaves as part of
try-catch and also enters
finally`.SystemExit
inherits BaseException
so that it is not mistakenly caught by catch
as a normal exception or error. (You can also consciously catch
.)
In other words, it can be considered that the shutdown process is performed at the place where the upper SystemExit
is received.is a shutdown process, so destructors and shutdown functions are executed, but
finally` as an exception mechanism is not passed.The difference is whether or not to handle ʻexit` as an exception.
PHP
You can find it in the explanation and user notes of ʻexit`.
Example 3 An example of a shutdown function or destructor being executed
>
<?php
class Foo
{
public function __destruct()
{
echo 'Destruct: ' . __METHOD__ . '()' . PHP_EOL;
}
}
>
function shutdown()
{
echo 'Shutdown: ' . __FUNCTION__ . '()' . PHP_EOL;
}
>
$foo = new Foo();
register_shutdown_function('shutdown');
>
exit();
echo 'This is not output.';
?>
The output of the above example is as follows.
Shutdown: shutdown()
Destruct: Foo::__destruct()
PHP: exit - Manual : User Contributed Notes
A side-note for the use of exit with finally: if you exit somewhere in a try block, the finally won't be executed. Could not sound obvious: for instance in Java you never issue an exit, at least a return in your controller; in PHP instead you could find yourself exiting from a controller method (e.g. in case you issue a redirect).
Here follows the POC:
<?php
echo "testing finally wit exit\n";
>
try {
echo "In try, exiting\n";
>
exit;
} catch(Exception $e) {
echo "catched\n";
} finally {
echo "in finally\n";
}
>
echo "In the end\n";
?>
This will print:
testing finally wit exit In try, exiting
There is also a way to throw an exception instead of exit and write the termination process with catch, but it seems that it is not a perfect alternative.
Naturally, I tried running it on PHP7, but the result was the same.
Python
Uchan Note: Differences between Python exit (), sys.exit (), os._exit () It is a glance if you look at it.
Since exceptions are thrown except for ʻos._exit (),
finally is also relevant. When it is said that ʻos._exit ()
is equivalent to PHP's ʻexit ()`, it looks a little different.
exit
4. Built-in constants — Python 2.7.x documentation
An object that prints a message like “Use quit () or Ctrl-D (ie EOF) to exit” when displayed and sends a
SystemExit
with the specified exit code when called. ..
sys.exit()
28.1. sys — System Parameters and Functions — Python 2.7.x Documentation
Exit
Python
. Since ʻexit ()sends a
SystemExit, it is possible to describe the termination process in the
finallyclause of the
trystatement, or to catch an exception at a higher level and interrupt the ʻexit
process. I can do it. (Omitted) Ultimately, ʻexit ()` is “only” throwing an exception, so when it is called from the main thread, it just terminates the process and does not block the exception.
6. Built-in exceptions — Python 2.7.x documentation
This exception is thrown by the sys.exit () function. If this exception is not handled, the Python interpreter will exit without displaying any tracebacks on the stack. If the associated value is a regular integer, it represents the system exit status (passed to the exit () function). If the value is None, the exit status is 0. For other types (such as strings), the value of that object is displayed and the exit status is 1.
The instance of this exception has the attribute code. This value is set to the exit status or error message (None by default). Also, this exception is not strictly an error, so it derives from BaseException instead of StandardError.
sys.exit () ensures that the cleanup process (finally clause of the try statement) is executed and that the script can be executed without risking the debugger getting out of control. Translated into exceptions. You can use the os._exit () function when you really need to exit immediately (for example, in a child process after calling fork ()).
This exception inherits from BaseException rather than from StandardError or Exception so that the code that catches the Exception doesn't accidentally catch it. This will steadily propagate this exception to the caller and terminate the interpreter.
Changed in version 2.5: Changed to inherit BaseException.
Exactly! is not it.
os._exit()
15.1. Os — Miscellaneous operating system interfaces — Python 2.7.x documentation
Exit the process with exit status n. At this time, the cleanup handler is not called and the standard I / O buffer is not flushed.
From the same blog.
It's a transition from the era without finally
to the era after implementation,
Combined with the operation of ʻexit` this time, I feel that the destructor has been re-recognized.
Recommended Posts