<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>exit(EXIT_FAILURE) vs. abort vs. terminate</title>
		<link>http://www.allegro.cc/forums/view/602770</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 09 Jan 2010 21:08:12 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi,</p><p>When to use exit(EXIT_FAILURE), abort or terminate?</p><p>I&#39;m not eben sure where&#39;s the difference between those three.</p><p><b>Edit:</b></p><p>I realized that terminate calls abort.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Martin Kalbfuß)</author>
		<pubDate>Sat, 09 Jan 2010 07:43:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Use exit(EXIT_FAILURE) if you want to exit normally and have an error code that you may be able to test from the shell. Use abort if you want to flag a critical error and generate a core dump.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Sat, 09 Jan 2010 17:17:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>exit() will call allegro_exit() for you, abort() will not.  exit() is basically a shortcut you can use when an error occurs, and exiting all the way through main is too much trouble.  abort() is for the more extreme cases.</p><p>If you&#39;re using C++, abort() won&#39;t call any destructors, while exit() will.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Sat, 09 Jan 2010 17:55:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><i>std::terminate</i> is a C++ feature that is typically called when an exception is thrown without a corresponding catch block.  Typically you would override this with something that outputs the trouble to your log.</p><p>There is also <i>std::unexpected_handler</i> which calls <i>std::terminate</i> by default.  <i>std::unexpected_handler</i> is called when you throw an exception that is not allowed by the throw specifiers.</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// foo may throw *only* int, char, or short.</span>
<span class="k1">void</span> foo<span class="k2">(</span><span class="k2">)</span> <span class="k1">throw</span><span class="k2">(</span><span class="k1">int</span>, <span class="k1">char</span>, <span class="k1">short</span><span class="k2">)</span>
<span class="k2">{</span>
    <span class="k1">throw</span> <span class="k1">float</span><span class="k2">(</span><span class="n">25</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>

<span class="k1">void</span> main<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
    <span class="c">// Here std::unexpected_handler will be called which then calls std::terminate which then calls abort.</span>
    foo<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>

    <span class="c">// Here std::terminate will be called which calls abort.</span>
    <span class="k1">throw</span> <span class="n">5</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

<a href="http://www.cplusplus.com/reference/std/exception/set_terminate/">std::set_terminate</a><br /><a href="http://www.cplusplus.com/reference/std/exception/set_unexpected/">std::set_unexpected</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Sat, 09 Jan 2010 18:18:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602770/846470#target">torhu</a> said:</div><div class="quote"><p>If you&#39;re using C++, abort() won&#39;t call any destructors, while exit() will.</p></div></div><p>
Also, abort() is not guaranteed to perform any form of cleanup, such as closing open file handles, flushing buffered but unwritten data or deleting temporary files. exit() is.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Sat, 09 Jan 2010 18:22:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for your answers. But there are still questions left.</p><p>Isn&#39;t a program termination without cleanup a bad thing? Even if the program doesn&#39;t work as it should?</p><p>Is the core dump the only benefit of using abort?</p><p>I&#39;m not sure if there&#39;s any reason to call abort or terminate directly.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Martin Kalbfuß)</author>
		<pubDate>Sat, 09 Jan 2010 19:37:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602770/846480#target">Martin Kalbfuß</a> said:</div><div class="quote"><p>Isn&#39;t a program termination without cleanup a bad thing?</p></div></div><p>
Perhaps. Under normal circumstances, yes. But you wouldn&#39;t call abort() under normal circumstances.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Is the core dump the only benefit of using abort?</p></div></div><p>
Isn&#39;t it enough?<br />The fact that the program terminates without trying to do extra cleanup can also be an advantage, in situations where trying to clean up could further destabilise the system (although that isn&#39;t likely these days, since all programs are fairly well encapsulated).</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>I&#39;m not sure if there&#39;s any reason to call abort or terminate directly.</p></div></div><p>
There is if you want a core dump.<br />If there is a (critical) error and you call exit(), then you won&#39;t know where the error occurred (the program will exit cleanly). If you call abort(), you&#39;ll be able to catch the error at the point where it occurs. It&#39;s a debugging aid, which is why it&#39;s normally called by assert().
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Sat, 09 Jan 2010 19:59:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So if I have a case where something can go wrong, I throw an exception. I can think of two cases. Please tell me what do you think about it.</p><p>1. I don&#39;t catch the exception. It&#39;s a programming error. I get the core dump and I&#39;m able to debug my program. This kind of error shouldn&#39;t ever get in touch with the enduser.</p><p>example:</p><p>an array index out of range.</p><p>2. I catch the exception to handle it. (retry with different settings, or output an error message). This kind of error depends on the surroundings. System settings, data files or user input. It&#39;s not a programming error. </p><p>example:</p><p>couldn&#39;t find a bitmap file.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Martin Kalbfuß)</author>
		<pubDate>Sat, 09 Jan 2010 21:08:12 +0000</pubDate>
	</item>
</rss>
