<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>desgrange.net &#187; erlang</title>
	<atom:link href="http://blog.desgrange.net/tag/erlang/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.desgrange.net</link>
	<description></description>
	<lastBuildDate>Mon, 30 Jan 2012 21:00:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Open Instant Messaging</title>
		<link>http://blog.desgrange.net/2009/06/22/open-instant-messaging/</link>
		<comments>http://blog.desgrange.net/2009/06/22/open-instant-messaging/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 22:00:00 +0000</pubDate>
		<dc:creator>laurent</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[XMPP]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[ejabberd]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[instant messaging]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[standards]]></category>

		<guid isPermaLink="false">http://blog.desgrange.net/?p=675</guid>
		<description><![CDATA[Email protocols history A long time ago there was several protocols to send a message from a computer to an other one. There was a protocol per network (the internet was not really born at that time). Those protocols were not compatible between each others. So for instance, if you were using FidoNet, you were [...]]]></description>
			<content:encoded><![CDATA[<h5>Email protocols history</h5>
<p>A long time ago there was several <a hreflang="en" href="http://en.wikipedia.org/wiki/Protocol_(computing)" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Protocol_computing?referer=');">protocols</a> to send a message from a computer to an other one. There was a protocol per network (the <a hreflang="en" href="http://en.wikipedia.org/wiki/Internet" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Internet?referer=');">internet</a> was not really born at that time). Those protocols were not compatible between each others. So for instance, if you were using <a hreflang="en" href="http://en.wikipedia.org/wiki/FidoNet" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/FidoNet?referer=');">FidoNet</a>, you were not able to send <a hreflang="en" href="http://en.wikipedia.org/wiki/E-mail" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/E-mail?referer=');">emails</a> to people using <a hreflang="en" href="http://en.wikipedia.org/wiki/BITNET" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/BITNET?referer=');">BITNET</a>. Hopefully some people created some gateways to transfer emails from a network to an other one (but it looks like it was quite a nightmare). At some point in time, <a hreflang="en" href="http://en.wikipedia.org/wiki/ARPANET" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/ARPANET?referer=');">ARPANET</a> and its email protocol became the standard and other protocols started vanishing. People were now able to communicate with each other easily.</p>
<h5>Instant messaging</h5>
<p><a hreflang="en" href="http://en.wikipedia.org/wiki/Instant_messaging" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Instant_messaging?referer=');">Instant messaging</a> (IM) appeared much more recently (email started in the late 60s, late 80s for IM). Personal IM became very well known in 1996 with <a hreflang="en" href="http://en.wikipedia.org/wiki/ICQ" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/ICQ?referer=');">ICQ</a> (I still have my ICQ account! (but nobody to talk to me on it anymore)). Then several other protocols appeared:</p>
<ul>
<li><a hreflang="en" href="http://en.wikipedia.org/wiki/Jabber" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Jabber?referer=');">Jabber</a> (1998, free and open protocol)</li>
<li><a hreflang="en" href="http://en.wikipedia.org/wiki/QQ" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/QQ?referer=');">QQ</a> (1998, closed and proprietary)</li>
<li><a hreflang="en" href="http://en.wikipedia.org/wiki/Yahoo!_Messenger" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Yahoo_Messenger?referer=');">Yahoo! Messenger</a> (1998, closed and proprietary)</li>
<li><a hreflang="en" href="http://en.wikipedia.org/wiki/MSN_Messenger" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/MSN_Messenger?referer=');">MSN Messenger</a> (1999, closed and proprietary)</li>
<li>…</li>
</ul>
<p>Of course you can&#8217;t chat with somebody using MSN Messenger if you use Yahoo! Messenger. ICQ was bought by <a hreflang="en" href="http://en.wikipedia.org/wiki/AOL" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/AOL?referer=');">AOL</a>, AOL created <a hreflang="en" href="http://en.wikipedia.org/wiki/AOL_Instant_Messenger" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/AOL_Instant_Messenger?referer=');">AIM</a> (AOL Instant Messenger) based on ICQ protocol. At some point people using ICQ were able to speak with people using AIM.</p>
<p>In 2004, XMPP, the protocol used by Jabber, became the official <a hreflang="en" href="http://en.wikipedia.org/wiki/Internet_Engineering_Task_Force" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Internet_Engineering_Task_Force?referer=');">IETF</a> instant messaging standard (IETF is the group defining the internet standards (like <a hreflang="en" href="http://en.wikipedia.org/wiki/HTTP" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/HTTP?referer=');">HTTP</a> for the web, <a hreflang="en" href="http://en.wikipedia.org/wiki/SMTP" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/SMTP?referer=');">SMTP</a> for emails, <a hreflang="en" href="http://en.wikipedia.org/wiki/FTP" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/FTP?referer=');">FTP</a> for file transfer)).</p>
<h5>Jabber/XMPP</h5>
<p>XMPP as been the standard for 5 years now, but still, most of my contacts are using MSN Messenger. Why? Because they also use a <a hreflang="en" href="http://en.wikipedia.org/wiki/Microsoft_Windows" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Microsoft_Windows?referer=');">Microsoft Windows</a> based computer and Microsoft MSN Messenger is the default instant messaging software installed on those computers.</p>
<p>XMPP allows creating gateways to other protocols (like MSN, Yahoo!, ICQ). The situation looks a bit like the email status in the old days. But the email standard imposed itself as the killer application of ARPANET. So, what will be the thing that will wipe out all proprietary protocols and impose the XMPP open standard?</p>
<p>The problem with standards is that it take time to be developed. History as shown that new versions of a standard are not implemented by all software vendors quickly, nor they are deployed as fast as possible. So changes to the standard must not happen everyday and having it quite right at version 1.0 take time.</p>
<p>In the meantime, other protocols evolve faster because vendors have a captive market and a better control on how the software is distributed/used (and they don&#8217;t have to wait for others to implement changes).</p>
<p>While XMPP was on the way to be standardized, other protocols got voice then video functionalities. I had a lot of hope in 1995 when <a hreflang="en" href="http://en.wikipedia.org/wiki/Google" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Google?referer=');">Google</a> released it&#8217;s own IM software <a hreflang="en" href="http://en.wikipedia.org/wiki/Google_Talk" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Google_Talk?referer=');">Google Talk</a>. GTalk is based on XMPP and add some voice extension and video extension later.</p>
<p>Then AOL started an experiment to allow XMPP connections to its network, Yahoo announced that they were interested by XMPP too. The chat in <a hreflang="en" href="http://en.wikipedia.org/wiki/Facebook" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Facebook?referer=');">Facebook</a> uses XMPP (but the network is closed, you can only talk to Facebook users), several other community websites do the same.</p>
<h5>Nowadays</h5>
<p>Last week, the specifications for voice/video in XMPP were released. The biggest missing features making people stay with their proprietary IM is going to be old story soon. But I&#8217;m not sure it will be enough to see a big migration to XMPP.</p>
<p>Those last few years, XMPP interest increased a lot and nearly all IM vendors are now looking at XMPP except <a hreflang="en" href="http://en.wikipedia.org/wiki/Microsoft" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Microsoft?referer=');">Microsoft</a>. It looks like you will soon have the choice between speaking to nearly everybody except MSN Messenger users or speaking only to MSN Messenger users.</p>
<p>Of course there are lots of softwares allowing you to use several accounts at the same time (so being connected to MSN, Yahoo! and Jabber at the same time). I also have several emails account. The difference is that from my professional email account I can send emails to everybody, same for my personal email. I&#8217;m choosing the email account I&#8217;m using depending on what my &#8220;role&#8221; is. If I want to send a message to a colleague, I will use my professional email address.</p>
<p>In IM, you can&#8217;t do that, except if you open several accounts on each protocol you use. Having a professional and a personal account on MSN, on Yahoo!, on AIM, on Jabber With all those protocols I currently have 9 accounts. And you know what? I like keeping things simple. For my email addresses I have started closing several accounts, keeping only the mandatory ones (my personal email address and the ones I have to use for my job). And I would really like doing the same for IM but you know what? Here I can&#8217;t do what I want. Why? Because if I close my MSN account, I will lose contact with a lot of people.</p>
<p>I feel a bit like in jail. Worse, I feel like my friends are in jail too but they are saying &#8220;Where do you see a jail? There&#8217;s only walls and fences&#8221;.</p>
<p>Anyway, lots of people are using <a hreflang="en" href="http://en.wikipedia.org/wiki/GMail" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/GMail?referer=');">GMail</a> now, and there is a chat embedded in GMail. Of course this chat uses Google Talk so it uses XMPP. Even if I don&#8217;t really like GMail, I prefer having my friends using GMail/Google Talk than Hotmail/MSN Messenger (or whatever the name of those services are this week).</p>
<h5>ejabberd</h5>
<p>Since XMPP is an open protocol, anybody can implement it. There are several XMPP clients (<a hreflang="en" href="http://en.wikipedia.org/wiki/Pidgin_%28software%29" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Pidgin_28software_29?referer=');">Pidgin</a>, <a hreflang="en" href="http://en.wikipedia.org/wiki/Adium" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Adium?referer=');">Adium</a>, <a hreflang="en" href="http://en.wikipedia.org/wiki/Kopete" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Kopete?referer=');">Kopete</a>, <a hreflang="en" href="http://en.wikipedia.org/wiki/Trillian_%28software%29" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Trillian_28software_29?referer=');">Trillian</a>, <a hreflang="en" href="http://en.wikipedia.org/wiki/IChat" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/IChat?referer=');">iChat</a>) and there are also several servers.</p>
<p>In XMPP servers there is well-known one: <a hreflang="en" href="http://en.wikipedia.org/wiki/Ejabberd" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Ejabberd?referer=');">ejabberd</a>. This server is <a hreflang="en" href="http://en.wikipedia.org/wiki/Open_source" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Open_source?referer=');">open source</a> and written in <a hreflang="en" href="http://en.wikipedia.org/wiki/Erlang_%28programming_language%29" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Erlang_28programming_language_29?referer=');">Erlang</a>. Ejabberd use the power of Erlang to be <a hreflang="en" href="http://en.wikipedia.org/wiki/Fault_tolerance" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Fault_tolerance?referer=');">fault-tolerant</a>, <a hreflang="en" href="http://en.wikipedia.org/wiki/Redundancy_%28engineering%29" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Redundancy_28engineering_29?referer=');">redundant</a>, <a hreflang="en" href="http://en.wikipedia.org/wiki/Scalability" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Scalability?referer=');">scalable</a>, &lt;add here any cool property a server should have&gt;.</p>
<p>And since XMPP is a decentralized system, I can install my own server (as I did for <a hreflang="en" href="/2009/03/16/Mail-Story-1%3A-Debian-Postfix-%28SMTP%29-Courier-%28IMAP%29-SSL/">my email server</a> for instance).</p>
<p>Installing ejabberd on <a hreflang="en" href="http://en.wikipedia.org/wiki/Debian" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Debian?referer=');">debian</a> is as easy as usual:</p>
<pre>$ sudo apt-get install ejabberd</pre>
<p>To configure it, you just need to change the domain name to serve in <code>/etc/ejabberd/ejabberd.cfg</code>. If your domain name is <code>example.org</code> change the following:</p>
<pre>%% Hostname {hosts, ["example.org"]}.</pre>
<p>And set the admin user:</p>
<pre>
%% Admin user
{acl, admin, {user, "admin_user_name", "example.org"}}.
</pre>
<p>Add a user with the following command:</p>
<pre>$ sudo ejabberdctl register user_name example.org password</pre>
<p>Restart the server. Done.</p>
<p>Of course there are a lot more parameters to change if you want to fine tune it. You may also need to create a <code>SRV</code> entry in your <a hreflang="en" href="http://en.wikipedia.org/wiki/Domain_Name_System" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Domain_Name_System?referer=');">DNS</a> if your server is not the one serving &#8220;example.org&#8221; (but &#8220;im.example.org&#8221; for example).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.desgrange.net/2009/06/22/open-instant-messaging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPhone + Nimbuzz + Freephonie</title>
		<link>http://blog.desgrange.net/2009/02/09/iphone-nimbuzz-freephonie/</link>
		<comments>http://blog.desgrange.net/2009/02/09/iphone-nimbuzz-freephonie/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 23:00:00 +0000</pubDate>
		<dc:creator>laurent</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[VoIP]]></category>
		<category><![CDATA[XMPP]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[freephonie]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[jabber]]></category>
		<category><![CDATA[nimbuzz]]></category>

		<guid isPermaLink="false">http://blog.desgrange.net/?p=645</guid>
		<description><![CDATA[Il y a quelques temps, j&#8217;avais parlé de Fring, un logiciel disponible sur iPhone permettant de faire de la messagerie instantanée ainsi que de la VoIP. J&#8217;avais essayé ce logiciel pour le côté VoIP avec la possibilité de l&#8217;utiliser avec mon compte SIP de chez Free. J&#8217;ai été déçu par le fait qu&#8217;il fallait tout [...]]]></description>
			<content:encoded><![CDATA[<p>Il y a quelques temps, <a href="/2008/12/15/iPhone-Fring-Freephonie/">j&#8217;avais parlé de Fring</a>, un logiciel disponible sur iPhone permettant de faire de la messagerie instantanée ainsi que de la VoIP.</p>
<p>J&#8217;avais essayé ce logiciel pour le côté <a hreflang="fr" href="http://fr.wikipedia.org/wiki/VoIP" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/VoIP?referer=');">VoIP</a> avec la possibilité de l&#8217;utiliser avec mon compte <a hreflang="fr" href="http://fr.wikipedia.org/wiki/Session_Initiation_Protocol" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/Session_Initiation_Protocol?referer=');">SIP</a> de chez <a hreflang="fr" href="http://fr.wikipedia.org/wiki/Free_(société)" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/Free_soci_t?referer=');">Free</a>. J&#8217;ai été déçu par le fait qu&#8217;il fallait tout d&#8217;abord se créer un compte chez <a hreflang="en" href="http://www.fring.com/" onclick="pageTracker._trackPageview('/outgoing/www.fring.com/?referer=');">Fring</a>, et que tout le trafic (mes appels donc) passait par leurs serveurs.</p>
<p>Ce que je cherche c&#8217;est un client SIP tout bête. Je ne veux pas de service supplémentaire derrière et je ne veux encore moins que mes listes de contacts et appels téléphoniques passent par un intermédiaire supplémentaire, dans lequel je n&#8217;ai aucune confiance.</p>
<p>Aujourd&#8217;hui j&#8217;ai essayé <a hreflang="fr" href="http://fr.wikipedia.org/wiki/Nimbuzz" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/Nimbuzz?referer=');">Nimbuzz</a> qui se présente fortement comme Fring (ce n&#8217;est donc pas de bonne augure).</p>
<p>Au premier lancement il demande de se créer un compte <a hreflang="fr" href="http://www.nimbuzz.com/fr/" onclick="pageTracker._trackPageview('/outgoing/www.nimbuzz.com/fr/?referer=');">Nimbuzz</a> :</p>
<p><a href="http://blog.desgrange.net/wp-content/uploads/2009/02/login.png"><img class="aligncenter size-full wp-image-845" title="login" src="http://blog.desgrange.net/wp-content/uploads/2009/02/login.png" alt="" width="320" height="480" /></a></p>
<p>L&#8217;interface graphique est plus sympa que celle de Fring (goûts personnels) :</p>
<p><a href="http://blog.desgrange.net/wp-content/uploads/2009/02/contacts.png"><img class="aligncenter size-full wp-image-846" title="contacts" src="http://blog.desgrange.net/wp-content/uploads/2009/02/contacts.png" alt="" width="320" height="480" /></a></p>
<p>Je vais pour configurer mon compte SIP mais rien. Nimbuzz ne fait pas SIP, en tout cas pas la version <a hreflang="fr" href="http://fr.wikipedia.org/wiki/IPhone" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/IPhone?referer=');">iPhone</a>. Il semblerait que cette fonctionnalité ne soit supportée que pour la version <a hreflang="fr" href="http://fr.wikipedia.org/wiki/Symbian_OS" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/Symbian_OS?referer=');">Symbian</a>.</p>
<p>J&#8217;ai configuré un de mes comptes <a hreflang="fr" href="http://fr.wikipedia.org/wiki/XMPP" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/XMPP?referer=');">XMPP</a> (<a hreflang="fr" href="http://fr.wikipedia.org/wiki/Jabber" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/Jabber?referer=');">Jabber</a>), petit coup de <a hreflang="fr" href="http://fr.wikipedia.org/wiki/Tcpdump" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/Tcpdump?referer=');">tcpdump</a>, toutes les données passent par les serveurs de Nimbuzz. Chose intéressante (et évidente), le protocole utilisé par l&#8217;application Nimbuzz est XMPP. C&#8217;est le standard de la messagerie instantanée, développé à l&#8217;origine pour/par <a hreflang="en" href="http://www.jabber.org" onclick="pageTracker._trackPageview('/outgoing/www.jabber.org?referer=');">Jabber</a>, utilisé maintenant dans de nombreuses applications (<a hreflang="fr" href="http://fr.wikipedia.org/wiki/Google_Talk" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/Google_Talk?referer=');">Google Talk</a>, <a hreflang="fr" href="http://fr.wikipedia.org/wiki/Lotus_Notes" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/Lotus_Notes?referer=');">Lotus Notes</a>, <a hreflang="fr" href="http://fr.wikipedia.org/wiki/Facebook" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/Facebook?referer=');">Facebook</a> Chat).</p>
<p>Récemment Nimbuzz cherchait des développeurs <a hreflang="fr" href="http://fr.wikipedia.org/wiki/Erlang_(langage)" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/Erlang_langage?referer=');">Erlang</a>, ce qui n&#8217;est pas étonnant quand on sait qu&#8217;un des serveurs XMPP les plus utilisés (<a hreflang="fr" href="http://fr.wikipedia.org/wiki/Ejabberd" onclick="pageTracker._trackPageview('/outgoing/fr.wikipedia.org/wiki/Ejabberd?referer=');">ejabberd</a>) est écrit en Erlang.</p>
<p>Bref, en ce qui me concerne, Nimbuzz n&#8217;a aucun intérêt. Pas de SIP, mes données passent par leurs serveurs alors que je veux une connexion &#8220;directe&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.desgrange.net/2009/02/09/iphone-nimbuzz-freephonie/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Exceptions in Erlang</title>
		<link>http://blog.desgrange.net/2009/01/05/exceptions-in-erlang/</link>
		<comments>http://blog.desgrange.net/2009/01/05/exceptions-in-erlang/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 22:00:00 +0000</pubDate>
		<dc:creator>laurent</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://blog.desgrange.net/?p=640</guid>
		<description><![CDATA[Exception In a programming language, an exception is something that could be generated when the system is behaving outside the normal execution path. An exception is mostly an error. In lots of programming languages, developers use exceptions as a meaningful information to do something or not. For example, while reading a file, an exception can [...]]]></description>
			<content:encoded><![CDATA[<h4>Exception</h4>
<p>In a <a hreflang="en" href="http://en.wikipedia.org/wiki/Programming_language" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Programming_language?referer=');">programming language</a>, an <a hreflang="en" href="http://en.wikipedia.org/wiki/Exception_handling" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Exception_handling?referer=');">exception</a> is something that could be generated when the system is behaving outside the normal execution path. An exception is mostly an error. In lots of programming languages, developers use exceptions as a meaningful information to do something or not.</p>
<p>For example, while reading a file, an exception can be generated because the file does not exists. The developer may choose to catch the exception and display a popup to ask the user to choose an other file, or the developer may not catch the exception because the file must have been there and if it&#8217;s not there it&#8217;s because something wrong is happening but the developer has no clue about what to do, so the best solution is to let the system crash (as opposed to try to do something and maybe enter in an inconsistent state).</p>
<h4>Exceptions in Erlang</h4>
<p>In <a hreflang="en" href="http://en.wikipedia.org/wiki/Erlang_(programming_language)" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Erlang_programming_language?referer=');">Erlang</a> there are exceptions too.</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">exceptions.erl</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="erlang" style="font-family:monospace;"><span class="sy3">-</span><span class="re2">module</span><span class="br0">&#40;</span>exceptions<span class="br0">&#41;</span><span class="sy1">.</span>
<span class="sy3">-</span><span class="re2">compile</span><span class="br0">&#40;</span><span class="br0">&#91;</span>export_all<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">.</span>
&nbsp;
<span class="re3">run</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Test exception 1 starting...~n&quot;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="re3">exception1</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Test exception 1 finished.~n&quot;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    ok<span class="sy1">.</span>
&nbsp;
<span class="re3">exception1</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
  <a href="http://erlang.org/doc/man/erlang.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/erlang.html?referer=');"><span class="kw5">erlang</span></a>:<span class="re3">foo</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy1">.</span></pre></div></div>
<p>Running the <a hreflang="en" href="http://en.wikipedia.org/wiki/Function_(computer_science)" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Function_computer_science?referer=');">function</a> <code>run</code> will run <code>exception1</code> which throws an exception (the function <code>foo</code> does not exist in module <code>erlang</code>) and is not catched by <code>run</code> (so the second <code>fwrite</code> is not displayed):</p>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="text" style="font-family:monospace;">$ erl -s exceptions run
Erlang (BEAM) emulator version 5.6.2 [source] [smp:2] [async-threads:0] [kernel-poll:false]
&nbsp;
Test exception 1 starting...
{&quot;init terminating in do_boot&quot;,{undef,[{erlang,foo,[]},{exceptions,run,0},{init,start_it,1},{init,start_em,1}]}}
&nbsp;
Crash dump was written to: erl_crash.dump
init terminating in do_boot ()</pre></div></div>
<h5>Types of exceptions</h5>
<p>In <a hreflang="en" href="http://www.erlang.org/" onclick="pageTracker._trackPageview('/outgoing/www.erlang.org/?referer=');">Erlang</a> there are 3 kinds of exceptions that can be generated:</p>
<ul>
<li>normal exceptions, user generated (<code>throw(Reason)</code>)</li>
<li>errors, something is going really wrong, should not be catched (<code>erlang:error(Reason)</code>)</li>
<li>exit, used to terminate current process (<code>exit(Reason)</code>)</li>
</ul>
<h5>Catching an exception</h5>
<ul>
<li>Catching an exception with <code>catch</code>:</li>
</ul>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="erlang" style="font-family:monospace;"><span class="re3">run</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Test exception 1 starting...~n&quot;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="re5">Result</span> <span class="sy3">=</span> <span class="br0">&#40;</span><span class="kw1">catch</span> <a href="http://erlang.org/doc/man/erlang.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/erlang.html?referer=');"><span class="kw5">erlang</span></a>:<span class="re3">foo</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Test exception 1 finished: ~p~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">Result</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    ok<span class="sy1">.</span></pre></div></div>
<p>Calling <code>run</code> displays:</p>
<div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="text" style="font-family:monospace;">Erlang (BEAM) emulator version 5.6.2 [source] [smp:2] [async-threads:0] [kernel-poll:false]
&nbsp;
Test exception 1 starting...
Test exception 1 finished: {'EXIT',
                               {undef,
                                   [{erlang,foo,[]},
                                    {exceptions,run,0},
                                    {init,start_it,1},
                                    {init,start_em,1}]}}</pre></div></div>
<p>and the process is still alive.</p>
<ul>
<li>Catching an exception with <code>try  catch</code>. The full syntax is something like this:</li>
</ul>
<div id="wpshdo_5" class="wp-synhighlighter-outer"><div id="wpshdt_5" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_5"></a><a id="wpshat_5" class="wp-synhighlighter-title" href="#codesyntax_5"  onClick="javascript:wpsh_toggleBlock(5)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_5" onClick="javascript:wpsh_code(5)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_5" onClick="javascript:wpsh_print(5)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_5" class="wp-synhighlighter-inner" style="display: block;"><pre class="erlang" style="font-family:monospace;"><span class="kw1">try</span> <a href="http://erlang.org/doc/man/erlang.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/erlang.html?referer=');"><span class="kw5">erlang</span></a>:<span class="re3">foo</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">of</span>
	<span class="re5">Any</span> <span class="sy1">-&gt;</span>
	    <span class="re5">Any</span>
    <span class="kw1">catch</span>
	error:<span class="me2">Reason</span> <span class="sy1">-&gt;</span>
	    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Error reason: ~p~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">Reason</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">;</span>
	throw:<span class="me2">Reason</span> <span class="sy1">-&gt;</span>
	    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Throw reason: ~p~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">Reason</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">;</span>
	exit:<span class="me2">Reason</span> <span class="sy1">-&gt;</span>
	    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Exit reason: ~p~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">Reason</span><span class="br0">&#93;</span><span class="br0">&#41;</span>
    <span class="kw1">after</span>
	<a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Doing some stuff no matter what happened.~n&quot;</span><span class="br0">&#41;</span>
    <span class="kw1">end</span><span class="sy1">.</span></pre></div></div>
<p><code>try</code> executes the given function and return it&#8217;s value (which can be pattern matched) if everything&#8217;s OK, if an exception is generated it goes in the the matching <code>catch</code> clause. In any case, it then goes inside the <code>after</code> block (similar to <code>finally</code> in <a hreflang="en" href="http://en.wikipedia.org/wiki/Java_(programming_language)" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Java_programming_language?referer=');">Java</a>).</p>
<p>Running the previous code output:</p>
<div id="wpshdo_6" class="wp-synhighlighter-outer"><div id="wpshdt_6" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_6"></a><a id="wpshat_6" class="wp-synhighlighter-title" href="#codesyntax_6"  onClick="javascript:wpsh_toggleBlock(6)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_6" onClick="javascript:wpsh_code(6)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_6" onClick="javascript:wpsh_print(6)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_6" class="wp-synhighlighter-inner" style="display: block;"><pre class="text" style="font-family:monospace;">Error reason: undef
Doing some stuff no matter what happened.</pre></div></div>
<p>(Calling a function that does not exists throws an <code>error</code>)</p>
<p>As everything in Erlang, <code>try  catch</code> returns a value (the executed function, the return the matching clause or the return of the matching exception clause).</p>
<h5>Examples</h5>
<p>Let see more examples.</p>
<div id="wpshdo_7" class="wp-synhighlighter-outer"><div id="wpshdt_7" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_7"></a><a id="wpshat_7" class="wp-synhighlighter-title" href="#codesyntax_7"  onClick="javascript:wpsh_toggleBlock(7)" title="Click to show/hide code block">exceptions.erl</a></td><td align="right"><a href="#codesyntax_7" onClick="javascript:wpsh_code(7)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_7" onClick="javascript:wpsh_print(7)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_7" class="wp-synhighlighter-inner" style="display: block;"><pre class="erlang" style="font-family:monospace;"><span class="sy3">-</span><span class="re2">module</span><span class="br0">&#40;</span>exceptions<span class="br0">&#41;</span><span class="sy1">.</span>
<span class="sy3">-</span><span class="re2">compile</span><span class="br0">&#40;</span><span class="br0">&#91;</span>export_all<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">.</span>
&nbsp;
<span class="re3">run</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <span class="re3">run</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="sy1">,</span> no_exception<span class="sy1">,</span> <span class="st_h">'catch'</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="re3">run</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="sy1">,</span> no_exception<span class="sy1">,</span> <span class="st_h">'try'</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="re3">run</span><span class="br0">&#40;</span><span class="nu0">3</span><span class="sy1">,</span> <span class="st_h">'throw'</span><span class="sy1">,</span> <span class="st_h">'catch'</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="re3">run</span><span class="br0">&#40;</span><span class="nu0">4</span><span class="sy1">,</span> <span class="st_h">'throw'</span><span class="sy1">,</span> <span class="st_h">'try'</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="re3">run</span><span class="br0">&#40;</span><span class="nu0">5</span><span class="sy1">,</span> <span class="st_h">'exit'</span><span class="sy1">,</span> <span class="st_h">'catch'</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="re3">run</span><span class="br0">&#40;</span><span class="nu0">6</span><span class="sy1">,</span> <span class="st_h">'exit'</span><span class="sy1">,</span> <span class="st_h">'try'</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="re3">run</span><span class="br0">&#40;</span><span class="nu0">7</span><span class="sy1">,</span> <span class="st_h">'error'</span><span class="sy1">,</span> <span class="st_h">'catch'</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="re3">run</span><span class="br0">&#40;</span><span class="nu0">8</span><span class="sy1">,</span> <span class="st_h">'error'</span><span class="sy1">,</span> <span class="st_h">'try'</span><span class="br0">&#41;</span><span class="sy1">,</span>
    ok<span class="sy1">.</span>
&nbsp;
<span class="re3">run</span><span class="br0">&#40;</span><span class="re5">ID</span><span class="sy1">,</span> <span class="re5">Exception_type</span><span class="sy1">,</span> <span class="re5">Handling_type</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;~p) Generating ~p, handled with ~p.~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">ID</span><span class="sy1">,</span> <span class="re5">Exception_type</span><span class="sy1">,</span> <span class="re5">Handling_type</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="re5">Fun</span> <span class="sy3">=</span> <span class="kw1">fun</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span> <span class="re3">exception</span><span class="br0">&#40;</span><span class="re5">Exception_type</span><span class="br0">&#41;</span> <span class="kw1">end</span><span class="sy1">,</span>
    <span class="re5">Result</span> <span class="sy3">=</span> <span class="re3">execute</span><span class="br0">&#40;</span><span class="re5">Handling_type</span><span class="sy1">,</span> <span class="re5">Fun</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;~p) Result: ~p~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">ID</span><span class="sy1">,</span> <span class="re5">Result</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">.</span>
&nbsp;
<span class="re3">exception</span><span class="br0">&#40;</span>no_exception<span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <span class="me1">ok</span><span class="sy1">;</span>
<span class="re3">exception</span><span class="br0">&#40;</span><span class="st_h">'throw'</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <span class="kw3">throw</span><span class="br0">&#40;</span><span class="st0">&quot;Throwed exception&quot;</span><span class="br0">&#41;</span><span class="sy1">;</span>
<span class="re3">exception</span><span class="br0">&#40;</span><span class="st_h">'exit'</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <span class="kw3">exit</span><span class="br0">&#40;</span><span class="st0">&quot;Exited&quot;</span><span class="br0">&#41;</span><span class="sy1">;</span>
<span class="re3">exception</span><span class="br0">&#40;</span><span class="st_h">'error'</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <a href="http://erlang.org/doc/man/erlang.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/erlang.html?referer=');"><span class="kw5">erlang</span></a>:<span class="re3">error</span><span class="br0">&#40;</span><span class="st0">&quot;Error generated&quot;</span><span class="br0">&#41;</span><span class="sy1">.</span>
&nbsp;
<span class="re3">execute</span><span class="br0">&#40;</span><span class="st_h">'catch'</span><span class="sy1">,</span> <span class="re5">Fun</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <span class="br0">&#40;</span><span class="kw1">catch</span> <span class="re5">F</span><span class="re3">un</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy1">;</span>
<span class="re3">execute</span><span class="br0">&#40;</span><span class="st_h">'try'</span><span class="sy1">,</span> <span class="re5">Fun</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <span class="kw1">try</span> <span class="re5">F</span><span class="re3">un</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="kw1">catch</span>
	<span class="re5">Error</span>:<span class="me2">Reason</span> <span class="sy1">-&gt;</span>
	    <span class="br0">&#123;</span><span class="re5">Error</span><span class="sy1">,</span> <span class="re5">Reason</span><span class="br0">&#125;</span>
    <span class="kw1">end</span><span class="sy1">.</span></pre></div></div>
<p>Output:</p>
<div id="wpshdo_8" class="wp-synhighlighter-outer"><div id="wpshdt_8" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_8"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#codesyntax_8"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_8" onClick="javascript:wpsh_code(8)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_8" onClick="javascript:wpsh_print(8)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_8" class="wp-synhighlighter-inner" style="display: block;"><pre class="text" style="font-family:monospace;">1) Generating no_exception, handled with 'catch'.
1) Result: ok
2) Generating no_exception, handled with 'try'.
2) Result: ok
3) Generating throw, handled with 'catch'.
3) Result: &quot;Throwed exception&quot;
4) Generating throw, handled with 'try'.
4) Result: {throw,&quot;Throwed exception&quot;}
5) Generating exit, handled with 'catch'.
5) Result: {'EXIT',&quot;Exited&quot;}
6) Generating exit, handled with 'try'.
6) Result: {exit,&quot;Exited&quot;}
7) Generating error, handled with 'catch'.
7) Result: {'EXIT',{&quot;Error generated&quot;,
                    [{exceptions,exception,1},
                     {exceptions,execute,2},
                     {exceptions,run,3},
                     {exceptions,run,0},
                     {init,start_it,1},
                     {init,start_em,1}]}}
8​) Generating error, handled with 'try'.
8​) Result: {error,&quot;Error generated&quot;}</pre></div></div>
<p>An interesting thing we can see here is that, in case of an <code>error</code>, <code>catch</code> get a stack trace which can be very useful for debugging but <code>try  catch</code> does not get it.</p>
<p>I prefer <code>try  catch</code> syntax (and it&#8217;s the recommended way to catch exceptions because you can choose what kind of exceptions you want to catch, <code>catch</code> catches everything) but it&#8217;s regrettable that it does not return the stack trace.</p>
<p>You can use <code>erlang:get_stacktrace</code> but it returns the stack trace from where you are calling it. If the exception is generated deep inside the function you are calling, <code>get_stacktrace</code> does not gives the root cause of the exception.</p>
<p>Having a stack trace is very useful but it make things a bit slower. I made a simple benchmark:</p>
<div id="wpshdo_9" class="wp-synhighlighter-outer"><div id="wpshdt_9" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_9"></a><a id="wpshat_9" class="wp-synhighlighter-title" href="#codesyntax_9"  onClick="javascript:wpsh_toggleBlock(9)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_9" onClick="javascript:wpsh_code(9)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_9" onClick="javascript:wpsh_print(9)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_9" class="wp-synhighlighter-inner" style="display: block;"><pre class="erlang" style="font-family:monospace;"><span class="re3">bench</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <span class="re5">Throw_fun1</span> <span class="sy3">=</span> <span class="kw1">fun</span><span class="br0">&#40;</span><span class="re5">_</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span> <span class="br0">&#40;</span><span class="kw1">catch</span> <span class="re3">exception</span><span class="br0">&#40;</span><span class="st_h">'throw'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">end</span><span class="sy1">,</span>
    <span class="re5">Error_fun1</span> <span class="sy3">=</span> <span class="kw1">fun</span><span class="br0">&#40;</span><span class="re5">_</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span> <span class="br0">&#40;</span><span class="kw1">catch</span> <span class="re3">exception</span><span class="br0">&#40;</span><span class="st_h">'error'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">end</span><span class="sy1">,</span>
    <span class="re5">Throw_fun2</span> <span class="sy3">=</span> <span class="kw1">fun</span><span class="br0">&#40;</span><span class="re5">_</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span> <span class="kw1">try</span> <span class="re3">exception</span><span class="br0">&#40;</span><span class="st_h">'throw'</span><span class="br0">&#41;</span> <span class="kw1">catch</span> <span class="re5">Error</span>:<span class="me2">Reason</span> <span class="sy1">-&gt;</span> <span class="br0">&#123;</span><span class="re5">Error</span><span class="sy1">,</span> <span class="re5">Reason</span><span class="br0">&#125;</span> <span class="kw1">end</span> <span class="kw1">end</span><span class="sy1">,</span>
    <span class="re5">Error_fun2</span> <span class="sy3">=</span> <span class="kw1">fun</span><span class="br0">&#40;</span><span class="re5">_</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span> <span class="kw1">try</span> <span class="re3">exception</span><span class="br0">&#40;</span><span class="st_h">'error'</span><span class="br0">&#41;</span> <span class="kw1">catch</span> <span class="re5">Error</span>:<span class="me2">Reason</span> <span class="sy1">-&gt;</span> <span class="br0">&#123;</span><span class="re5">Error</span><span class="sy1">,</span> <span class="re5">Reason</span><span class="br0">&#125;</span> <span class="kw1">end</span> <span class="kw1">end</span><span class="sy1">,</span>
    <span class="re5">Seq</span> <span class="sy3">=</span> <a href="http://erlang.org/doc/man/lists.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/lists.html?referer=');"><span class="kw5">lists</span></a>:<span class="re3">seq</span><span class="br0">&#40;</span>1<span class="sy1">,</span> 100000<span class="br0">&#41;</span><span class="sy1">,</span>
    <a href="http://erlang.org/doc/man/timer.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/timer.html?referer=');"><span class="kw5">timer</span></a>:<span class="re3">sleep</span><span class="br0">&#40;</span>1000<span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="br0">&#123;</span><span class="re5">Time_throw1</span><span class="sy1">,</span> <span class="re5">_</span><span class="br0">&#125;</span> <span class="sy3">=</span> <a href="http://erlang.org/doc/man/timer.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/timer.html?referer=');"><span class="kw5">timer</span></a>:<span class="re3">tc</span><span class="br0">&#40;</span>lists<span class="sy1">,</span> foreach<span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">Throw_fun1</span><span class="sy1">,</span> <span class="re5">Seq</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="br0">&#123;</span><span class="re5">Time_error1</span><span class="sy1">,</span> <span class="re5">_</span><span class="br0">&#125;</span> <span class="sy3">=</span> <a href="http://erlang.org/doc/man/timer.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/timer.html?referer=');"><span class="kw5">timer</span></a>:<span class="re3">tc</span><span class="br0">&#40;</span>lists<span class="sy1">,</span> foreach<span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">Error_fun1</span><span class="sy1">,</span> <span class="re5">Seq</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="br0">&#123;</span><span class="re5">Time_throw2</span><span class="sy1">,</span> <span class="re5">_</span><span class="br0">&#125;</span> <span class="sy3">=</span> <a href="http://erlang.org/doc/man/timer.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/timer.html?referer=');"><span class="kw5">timer</span></a>:<span class="re3">tc</span><span class="br0">&#40;</span>lists<span class="sy1">,</span> foreach<span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">Throw_fun2</span><span class="sy1">,</span> <span class="re5">Seq</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="br0">&#123;</span><span class="re5">Time_error2</span><span class="sy1">,</span> <span class="re5">_</span><span class="br0">&#125;</span> <span class="sy3">=</span> <a href="http://erlang.org/doc/man/timer.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/timer.html?referer=');"><span class="kw5">timer</span></a>:<span class="re3">tc</span><span class="br0">&#40;</span>lists<span class="sy1">,</span> foreach<span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">Error_fun2</span><span class="sy1">,</span> <span class="re5">Seq</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Throw (catch): ~p micro seconds~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">Time_throw1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Error (catch): ~p micro seconds~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">Time_error1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Throw (try): ~p micro seconds~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">Time_throw2</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Error (try): ~p micro seconds~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">Time_error2</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">,</span>
    ok<span class="sy1">.</span></pre></div></div>
<p>Results (for 100 000 calls):</p>
<div id="wpshdo_10" class="wp-synhighlighter-outer"><div id="wpshdt_10" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_10"></a><a id="wpshat_10" class="wp-synhighlighter-title" href="#codesyntax_10"  onClick="javascript:wpsh_toggleBlock(10)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_10" onClick="javascript:wpsh_code(10)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_10" onClick="javascript:wpsh_print(10)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_10" class="wp-synhighlighter-inner" style="display: block;"><pre class="text" style="font-family:monospace;">Throw (catch): 73920 micro seconds
Error (catch): 169576 micro seconds
Throw (try): 64118 micro seconds
Error (try): 63125 micro seconds</pre></div></div>
<p>Throwing an error or an exception take the same amount of time but using <code>catch</code> on an error is 2.5 times slower than using <code>try  catch</code> (my quick conclusion on that is because <code>catch</code> generates a stack trace).</p>
<p>I would like having only one way of catching exceptions (no <code>catch</code>, only <code>try  catch</code>) and a way to specify if I want a stack trace or not in case of error. Maybe something like this:</p>
<div id="wpshdo_11" class="wp-synhighlighter-outer"><div id="wpshdt_11" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_11"></a><a id="wpshat_11" class="wp-synhighlighter-title" href="#codesyntax_11"  onClick="javascript:wpsh_toggleBlock(11)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_11" onClick="javascript:wpsh_code(11)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_11" onClick="javascript:wpsh_print(11)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_11" class="wp-synhighlighter-inner" style="display: block;"><pre class="erlang" style="font-family:monospace;"><span class="kw1">try</span> <span class="re5">F</span><span class="re3">un</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="kw1">catch</span>
    <span class="re5">Error</span>:<span class="me2">Reason</span>:<span class="me2">Stack</span> <span class="sy1">-&gt;</span>
        <span class="br0">&#123;</span><span class="re5">Error</span>:<span class="me2">Reason</span>:<span class="me2">Stack</span><span class="br0">&#125;</span>
<span class="kw1">end</span><span class="sy1">.</span></pre></div></div>
<p>If a catch clause is waiting for 3 elements (<code>Error</code>, <code>Reason</code>, <code>Stack</code>), the compiler add the necessary stuff to call the <code>Fun</code> with a stack trace. If there are only 2 elements (<code>Error</code>, <code>Reason</code>), keep the actual behavior.</p>
<h5>Do we really need exceptions in Erlang?</h5>
<p>Exceptions may be useful but we can achieve the same goal in Erlang in other ways. Lots of functions have a signature like: <code>{ok, Value} | {error, Reason}</code>.</p>
<p>Those functions have different outputs between the normal case and the exceptional case. Combined with <code>case</code> we get the same behavior as catching an exception. If we don&#8217;t use <code>case</code>, we get a <code>badmatch</code> error.</p>
<div id="wpshdo_12" class="wp-synhighlighter-outer"><div id="wpshdt_12" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_12"></a><a id="wpshat_12" class="wp-synhighlighter-title" href="#codesyntax_12"  onClick="javascript:wpsh_toggleBlock(12)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_12" onClick="javascript:wpsh_code(12)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_12" onClick="javascript:wpsh_print(12)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_12" class="wp-synhighlighter-inner" style="display: block;"><pre class="erlang" style="font-family:monospace;"><span class="re3">case_way</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <span class="re5">Fun</span> <span class="sy3">=</span> <span class="kw1">fun</span><span class="br0">&#40;</span>ok<span class="br0">&#41;</span> <span class="sy1">-&gt;</span> <span class="br0">&#123;</span>ok<span class="sy1">,</span> <span class="st0">&quot;It works&quot;</span><span class="br0">&#125;</span><span class="sy1">;</span>
	     <span class="br0">&#40;</span>nok<span class="br0">&#41;</span> <span class="sy1">-&gt;</span> <span class="br0">&#123;</span>error<span class="sy1">,</span> <span class="st0">&quot;It does not work&quot;</span><span class="br0">&#125;</span>
	  <span class="kw1">end</span><span class="sy1">,</span>
    <span class="re3">run_case</span><span class="br0">&#40;</span>1<span class="sy1">,</span> <span class="re5">Fun</span><span class="sy1">,</span> ok<span class="br0">&#41;</span><span class="sy1">,</span>
    <span class="re3">run_case</span><span class="br0">&#40;</span>2<span class="sy1">,</span> <span class="re5">Fun</span><span class="sy1">,</span> nok<span class="br0">&#41;</span><span class="sy1">,</span>
    ok<span class="sy1">.</span>
&nbsp;
<span class="re3">run_case</span><span class="br0">&#40;</span><span class="re5">ID</span><span class="sy1">,</span> <span class="re5">Fun</span><span class="sy1">,</span> <span class="re5">Arg</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <span class="kw1">case</span> <span class="re5">F</span><span class="re3">un</span><span class="br0">&#40;</span><span class="re5">Arg</span><span class="br0">&#41;</span> <span class="kw1">of</span>
	<span class="br0">&#123;</span>error<span class="sy1">,</span> <span class="re5">Reason</span><span class="br0">&#125;</span> <span class="sy1">-&gt;</span>
	    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;~p) Error: ~p~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">ID</span><span class="sy1">,</span> <span class="re5">Reason</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">;</span>
	<span class="br0">&#123;</span>ok<span class="sy1">,</span> <span class="re5">Value</span><span class="br0">&#125;</span> <span class="sy1">-&gt;</span>
	    <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;~p) Value: ~p~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">ID</span><span class="sy1">,</span> <span class="re5">Value</span><span class="br0">&#93;</span><span class="br0">&#41;</span>
    <span class="kw1">end</span><span class="sy1">.</span></pre></div></div>
<p>Running <code>case_way</code>:</p>
<div id="wpshdo_13" class="wp-synhighlighter-outer"><div id="wpshdt_13" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_13"></a><a id="wpshat_13" class="wp-synhighlighter-title" href="#codesyntax_13"  onClick="javascript:wpsh_toggleBlock(13)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_13" onClick="javascript:wpsh_code(13)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_13" onClick="javascript:wpsh_print(13)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_13" class="wp-synhighlighter-inner" style="display: block;"><pre class="text" style="font-family:monospace;">1) Value: &quot;It works&quot;
2) Error: &quot;It does not work&quot;</pre></div></div>
<p>Boons:</p>
<ul>
<li>Way much faster than exceptions (around 4 times faster according to my quick bench)</li>
<li>No specific syntax</li>
</ul>
<p>Banes:</p>
<ul>
<li>Normal case needs some encapsulation like <code>{ok, Value}</code> instead or returning <code>Value</code> directly (not doing so may lead to unknown states if the return is not pattern matched for errors)</li>
<li>No convention (you can return {exit, Type, Reason} if you want, or anything you want, developer needs to read carefully documentation of each function before using it)</li>
</ul>
<p>In <a hreflang="en" href="http://www.amazon.com/exec/obidos/ASIN/193435600X" onclick="pageTracker._trackPageview('/outgoing/www.amazon.com/exec/obidos/ASIN/193435600X?referer=');">Joe Armstrong&#8217;s book</a>, he says that usually people use <code>{error, Reason}</code> when an error occurs quite often and exceptions for less frequent errors. It makes sense, but I do not completely agree with <a hreflang="en" href="http://armstrongonsoftware.blogspot.com/" onclick="pageTracker._trackPageview('/outgoing/armstrongonsoftware.blogspot.com/?referer=');">Joe</a> on that matter. When you are developing a software for yourself (or you firm), you may know how your code is used so you know if an error occurs often or not. But when you are developing software for other developers, you don&#8217;t know how they are going to use it, it&#8217;s more difficult to &#8220;predict&#8221; if the error is going to be thrown often or not.</p>
<p>So I tend to prefer throwing exceptions (maybe I have used Java for too long). I think the added syntax is necessary in order to keep thing clear, homogeneous and easy to use. I would love seeing a version of Erlang without functions having a <code>{error, Reason}</code> thing in their return signature (but something like <code>throw(Reason)</code>).</p>
<p>Complete source code associated with this post (you can do whatever you want with it): <a href="http://blog.desgrange.net/wp-content/uploads/2009/01/exceptions.erl">exceptions.erl</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.desgrange.net/2009/01/05/exceptions-in-erlang/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Object Oriented Programming</title>
		<link>http://blog.desgrange.net/2008/12/22/object-oriented-programming/</link>
		<comments>http://blog.desgrange.net/2008/12/22/object-oriented-programming/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 22:00:00 +0000</pubDate>
		<dc:creator>laurent</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[object oriented programming]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://blog.desgrange.net/?p=637</guid>
		<description><![CDATA[Programming languages use different paradigms, nowadays the most used is the Object Oriented paradigm. Object Oriented Programming was an attempt to simplify the creation of softwares because they were more and more complex. The idea was to model the software with objects (a car for example) with some attributes (it has wheels, a motor, etc.) [...]]]></description>
			<content:encoded><![CDATA[<p><a hreflang="en" href="http://en.wikipedia.org/wiki/Programming_language" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Programming_language?referer=');">Programming languages</a> use different <a hreflang="en" href="http://en.wikipedia.org/wiki/Programming_paradigm" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Programming_paradigm?referer=');">paradigms</a>, nowadays the most used is the <a hreflang="en" href="http://en.wikipedia.org/wiki/Object-oriented_programming" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Object-oriented_programming?referer=');">Object Oriented paradigm</a>. Object Oriented Programming was an attempt to simplify the creation of <a hreflang="en" href="http://en.wikipedia.org/wiki/Software" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Software?referer=');">softwares</a> because they were more and more complex.</p>
<p>The idea was to model the software with objects (a car for example) with some attributes (it has wheels, a motor, etc.) and some methods (start, move forward, etc.). Objects alone are not enough, they need to be able to communicate with each other by sending messages.</p>
<p>I have worked for several years with <a hreflang="en" href="http://en.wikipedia.org/wiki/Java_(programming_language)" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Java_programming_language?referer=');">Java</a>, a well known OOP language, and I&#8217;m now working with <a hreflang="en" href="http://en.wikipedia.org/wiki/Erlang_(programming_language)" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Erlang_programming_language?referer=');">Erlang</a>, a <a hreflang="en" href="http://en.wikipedia.org/wiki/Functional_programming" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Functional_programming?referer=');">functional language</a>.</p>
<p>A functional language has functions as first class citizens. Functions should be stateless, they get some parameters and return a deterministic result. Seems not enough to design complex softwares.</p>
<p>An other feature of Erlang is the <a hreflang="en" href="http://en.wikipedia.org/wiki/Concurrent_computing" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Concurrent_computing?referer=');">concurrent paradigm</a>. It allows the language to do a lot of tasks in parallel. At first, most people think about this feature has a great tool to distribute calculation and/or use all the <a hreflang="en" href="http://en.wikipedia.org/wiki/Multi-core" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Multi-core?referer=');">cores</a> of a modern <a hreflang="en" href="http://en.wikipedia.org/wiki/CPU" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/CPU?referer=');">CPU</a> with no burden. I thought the same thing. But this is not the aim of Erlang. Concurrent processes are used for software design. A process is a system, mostly representing a real life system, sending and receiving messages from other processes.</p>
<p>After using Erlang for a while (I&#8217;m not a very fast thinker, or maybe I just don&#8217;t think too often <img src='http://blog.desgrange.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ), I realized that Erlang is the most Object Oriented Language I have ever seen.</p>
<p>It matches the definition of OOP more than any other language. Objects are processes (with an internal state) talking to each others by sending messages.</p>
<p>Why? Because Erlang allows you to have several thousands threads (objects) running simultaneously, where other languages allows you to have several thousand objects running in few concurrent threads (so if you have X threads, only X objects are running simultaneously). In Erlang objects are really independent from each others, while in languages like C++, C# or Java, objects are executed sequentially.</p>
<p>Interestingly, if they share the same base idea, those languages need a completely different approach. One of the big differences is that talking between objects in Erlang is done by passing messages while it&#8217;s done by methods calls in traditional OOP languages.</p>
<p>In those languages we do not deal very often with multiple threads and we tend to not have threads talking with each others because it needs locks and synchronization. Erlang deals with that very easily.</p>
<p>So in an OOP language like Java, when a thread dies, a lot of objects dies, maybe the whole software, that&#8217;s may be not so bad (kind of <a hreflang="en" href="http://en.wikipedia.org/wiki/Fail_Fast" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Fail_Fast?referer=');">fail fast</a> approach). In Erlang, when a process dies, it&#8217;s just an object, everything else is still running (when a car crash, the neighborhood does not disappear), but this can lead to some unknown state (imagine a firm where the boss dies and nobodies notice it (and are glad to not receive orders anymore <img src='http://blog.desgrange.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> )). Erlang provides some tools to manage that. Processes can be linked together, when a process dies, it sends a death note (sorry <img src='http://blog.desgrange.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ) to processes it was linked to. When a process receives such a message, if it does not know how to handle it, it dies too (my wife is dead, I&#8217;m lost, I must commit suicide), but of course it may know what to do with the message, like restarting a process like the dead one (let&#8217;s find a new wife). In Erlang&#8217;s libraries there are such modules, called supervisors, specialized in re-spawning a process when it dies. It may looks like overhead, but I think it&#8217;s very useful to create robust and fault-tolerant applications.</p>
<p>Anyway, I have the feeling that in the Erlang&#8217;s community, there is a strong opinion against OOP. I think I will have understood how to design softwares in Erlang faster if people told me first that Erlang is true OOP and why and how. Moreover, it may bring more people to Erlang to develop the language and reduce the number of people asking for such and such OOP feature that are already in Erlang but not the way they are thinking about it.</p>
<p><a hreflang="en" href="http://en.wikipedia.org/wiki/Ralph_Johnson" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Ralph_Johnson?referer=');">Ralph Johnson</a> has an interesting (and short enough) article about <a hreflang="en" href="http://www.cincomsmalltalk.com/userblogs/ralph/blogView?entry=3364027251" onclick="pageTracker._trackPageview('/outgoing/www.cincomsmalltalk.com/userblogs/ralph/blogView?entry=3364027251&amp;referer=');">Erlang being the next Java</a>.</p>
<p><a hreflang="en" href="http://www.planeterlang.org/story.php?title=Thinking_About_Concurrency_Oriented_Programming" onclick="pageTracker._trackPageview('/outgoing/www.planeterlang.org/story.php?title=Thinking_About_Concurrency_Oriented_Programming&amp;referer=');">Same thing was discussed on PlanetErlang</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.desgrange.net/2008/12/22/object-oriented-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Erlang hot swapping</title>
		<link>http://blog.desgrange.net/2008/12/01/erlang-hot-swapping/</link>
		<comments>http://blog.desgrange.net/2008/12/01/erlang-hot-swapping/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 22:00:00 +0000</pubDate>
		<dc:creator>laurent</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://blog.desgrange.net/?p=634</guid>
		<description><![CDATA[I started using the erlang programming language few months ago. Erlang is a wonderful language if you want to do reliable, distributed, highly concurrent, fault-tolerant, soft-real-time, highly available, hot swapping applications . So, today I will give you a glimpse on one cool functionality of Erlang: the ability to update your code without stopping your [...]]]></description>
			<content:encoded><![CDATA[<p>I started using the <a hreflang="en" href="http://en.wikipedia.org/wiki/Erlang_(programming_language)" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Erlang_programming_language?referer=');">erlang</a> <a hreflang="en" href="http://en.wikipedia.org/wiki/Programming_language" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Programming_language?referer=');">programming language</a> few months ago. <a hreflang="en" href="http://www.erlang.org/" onclick="pageTracker._trackPageview('/outgoing/www.erlang.org/?referer=');">Erlang</a> is a wonderful language if you want to do reliable, <a hreflang="en" href="http://en.wikipedia.org/wiki/Distributed_computing" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Distributed_computing?referer=');">distributed</a>, highly <a hreflang="en" href="http://en.wikipedia.org/wiki/Concurrent_computing" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Concurrent_computing?referer=');">concurrent</a>, <a hreflang="en" href="http://en.wikipedia.org/wiki/Fault_tolerance" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Fault_tolerance?referer=');">fault-tolerant</a>, <a hreflang="en" href="http://en.wikipedia.org/wiki/Soft_real-time" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Soft_real-time?referer=');">soft-real-time</a>, <a hreflang="en" href="http://en.wikipedia.org/wiki/High_availability" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/High_availability?referer=');">highly available</a>, <a hreflang="en" href="http://en.wikipedia.org/wiki/Hot_swapping" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Hot_swapping?referer=');">hot swapping</a> applications <img src='http://blog.desgrange.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p>So, today I will give you a glimpse on one cool functionality of Erlang: the ability to update your code without stopping your application. Here it&#8217;s just about how it works for a process (the principle is the same for an application except that you have much more things to take into account when you have several thousands process talking to each other).</p>
<h5>THE CODE</h5>
<p>We are going to use a simple piece of code. We want a server thread listening for messages (each message is printed on the console with a sequence number), and a client thread, sending a message to the server every second. Here is the code (I usually don&#8217;t use comments but I put some here because you may not understand erlang code):</p>
<div id="wpshdo_14" class="wp-synhighlighter-outer"><div id="wpshdt_14" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_14"></a><a id="wpshat_14" class="wp-synhighlighter-title" href="#codesyntax_14"  onClick="javascript:wpsh_toggleBlock(14)" title="Click to show/hide code block">code_reload.erl</a></td><td align="right"><a href="#codesyntax_14" onClick="javascript:wpsh_code(14)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_14" onClick="javascript:wpsh_print(14)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_14" class="wp-synhighlighter-inner" style="display: block;"><pre class="erlang" style="font-family:monospace;"><span class="sy3">-</span><span class="re2">module</span><span class="br0">&#40;</span>code_reload<span class="br0">&#41;</span><span class="sy1">.</span> <span class="co1">% This is the module declaration.</span>
<span class="sy3">-</span><span class="re2">export</span><span class="br0">&#40;</span><span class="br0">&#91;</span>start_server<span class="sy3">/</span><span class="nu0">0</span><span class="sy1">,</span> start_client<span class="sy3">/</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">.</span> <span class="co1">% Exporting functions allow them to be called from outside the module.</span>
<span class="sy3">-</span><span class="re2">export</span><span class="br0">&#40;</span><span class="br0">&#91;</span>server_loop<span class="sy3">/</span>1<span class="sy1">,</span> client_loop<span class="sy3">/</span>1<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">.</span>
&nbsp;
<span class="re3">start_server</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <span class="kw3">spawn</span><span class="br0">&#40;</span>?<span class="re4">MODULE</span><span class="sy1">,</span> server_loop<span class="sy1">,</span> <span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">.</span> <span class="co1">% This function calls the server_loop function in a new thread.</span>
&nbsp;
<span class="re3">server_loop</span><span class="br0">&#40;</span><span class="re5">Count</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <span class="kw1">receive</span> <span class="co1">% Wait for messages</span>
        <span class="br0">&#123;</span><span class="re5">From</span><span class="sy1">,</span> quit<span class="br0">&#125;</span> <span class="sy1">-&gt;</span>
            <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Received quit command from ~p~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">From</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">,</span>
            ok<span class="sy1">;</span>
        <span class="br0">&#123;</span><span class="re5">From</span><span class="sy1">,</span> <span class="re5">Message</span><span class="br0">&#125;</span> <span class="sy1">-&gt;</span>
            <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;~p: Server received message ~p~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">Count</span><span class="sy1">,</span> <span class="kw3">self</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy1">,</span> <span class="re5">Message</span><span class="sy1">,</span> <span class="re5">From</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">,</span> <span class="co1">% Display the message we received.</span>
            ?<span class="re4">MODULE</span>:<span class="re3">server_loop</span><span class="br0">&#40;</span><span class="re5">Count</span><span class="br0">&#41;</span><span class="sy1">;</span> <span class="co1">% Call the same function again to wait for an other message.</span>
    	<span class="re5">_</span> <span class="sy1">-&gt;</span>
            <span class="kw3">throw</span><span class="br0">&#40;</span>unexpected_message<span class="br0">&#41;</span>
    <span class="kw1">end</span><span class="sy1">.</span>
&nbsp;
<span class="re3">start_client</span><span class="br0">&#40;</span><span class="re5">ServerPid</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <span class="kw3">spawn</span><span class="br0">&#40;</span>?<span class="re4">MODULE</span><span class="sy1">,</span> client_loop<span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">ServerPid</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">.</span>
&nbsp;
<span class="re3">client_loop</span><span class="br0">&#40;</span><span class="re5">ServerPid</span><span class="br0">&#41;</span> <span class="sy1">-&gt;</span>
    <span class="kw1">receive</span>
        <span class="br0">&#123;</span><span class="re5">From</span><span class="sy1">,</span> quit<span class="br0">&#125;</span> <span class="sy1">-&gt;</span>
            <a href="http://erlang.org/doc/man/io.html" onclick="pageTracker._trackPageview('/outgoing/erlang.org/doc/man/io.html?referer=');"><span class="kw5">io</span></a>:<span class="re3">fwrite</span><span class="br0">&#40;</span><span class="st0">&quot;Received quit command from ~p~n&quot;</span><span class="sy1">,</span> <span class="br0">&#91;</span><span class="re5">From</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy1">,</span>
            ok
    <span class="kw1">after</span> <span class="nu0">1000</span> <span class="sy1">-&gt;</span> <span class="co1">% If no messages were received after 1 second, send a message to the server.</span>
            <span class="re5">ServerPid</span> <span class="sy3">!</span> <span class="br0">&#123;</span><span class="kw3">self</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy1">,</span> <span class="kw3">now</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#125;</span><span class="sy1">,</span>
            ?<span class="re4">MODULE</span>:<span class="re3">client_loop</span><span class="br0">&#40;</span><span class="re5">ServerPid</span><span class="br0">&#41;</span>
    <span class="kw1">end</span><span class="sy1">.</span></pre></div></div>
<p>Let&#8217;s start an erlang shell:</p>
<div id="wpshdo_15" class="wp-synhighlighter-outer"><div id="wpshdt_15" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_15"></a><a id="wpshat_15" class="wp-synhighlighter-title" href="#codesyntax_15"  onClick="javascript:wpsh_toggleBlock(15)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_15" onClick="javascript:wpsh_code(15)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_15" onClick="javascript:wpsh_print(15)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_15" class="wp-synhighlighter-inner" style="display: block;"><pre class="text" style="font-family:monospace;">$ erl
Erlang (BEAM) emulator version 5.6.2 [source] [smp:2] [async-threads:0] [kernel-poll:false]
&nbsp;
Eshell V5.6.2  (abort with ^G)
1&gt; c(code_reload). % This compile the module code_reload.
{ok,code_reload}
2&gt; ServerPid = code_reload:start_server(). % Start the server and assign the server process id to the variable ServerPid.
&lt;0.37.0&gt;
3&gt; ClientPid = code_reload:start_client(ServerPid).
&lt;0.39.0&gt;
0: Server &lt;0.65.0&gt; received message {1211,11183,231032} from &lt;0.67.0&gt;
0: Server &lt;0.65.0&gt; received message {1211,11184,232031} from &lt;0.67.0&gt;
0: Server &lt;0.65.0&gt; received message {1211,11185,233029} from &lt;0.67.0&gt;
0: Server &lt;0.65.0&gt; received message {1211,11186,234031} from &lt;0.67.0&gt;
0: Server &lt;0.65.0&gt; received message {1211,11187,235030} from &lt;0.67.0&gt;</pre></div></div>
<p>We can see the server printing the messages. But oups, there is a bug! I forgot to increment the counter so each line start with &#8220;0&#8243; instead of an incrementing number.</p>
<p>Let&#8217;s change the following line in the code:</p>
<div id="wpshdo_16" class="wp-synhighlighter-outer"><div id="wpshdt_16" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_16"></a><a id="wpshat_16" class="wp-synhighlighter-title" href="#codesyntax_16"  onClick="javascript:wpsh_toggleBlock(16)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_16" onClick="javascript:wpsh_code(16)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_16" onClick="javascript:wpsh_print(16)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_16" class="wp-synhighlighter-inner" style="display: block;"><pre class="erlang" style="font-family:monospace;">?<span class="re4">MODULE</span>:<span class="re3">server_loop</span><span class="br0">&#40;</span><span class="re5">Count</span> <span class="sy3">+</span> 1<span class="br0">&#41;</span><span class="sy1">;</span></pre></div></div>
<p>We need to go back to the shell and compile the new code:</p>
<div id="wpshdo_17" class="wp-synhighlighter-outer"><div id="wpshdt_17" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_17"></a><a id="wpshat_17" class="wp-synhighlighter-title" href="#codesyntax_17"  onClick="javascript:wpsh_toggleBlock(17)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_17" onClick="javascript:wpsh_code(17)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_17" onClick="javascript:wpsh_print(17)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_17" class="wp-synhighlighter-inner" style="display: block;"><pre class="text" style="font-family:monospace;">4&gt; c(code_reload).
{ok,code_reload}
0: Server &lt;0.65.0&gt; received message {1211,11188,236023} from &lt;0.67.0&gt;
0: Server &lt;0.65.0&gt; received message {1211,11189,237031} from &lt;0.67.0&gt;
1: Server &lt;0.65.0&gt; received message {1211,11190,238021} from &lt;0.67.0&gt;
2: Server &lt;0.65.0&gt; received message {1211,11191,239031} from &lt;0.67.0&gt;
3: Server &lt;0.65.0&gt; received message {1211,11192,240018} from &lt;0.67.0&gt;
4: Server &lt;0.65.0&gt; received message {1211,11193,241022} from &lt;0.67.0&gt;
5: Server &lt;0.65.0&gt; received message {1211,11194,242021} from &lt;0.67.0&gt;
6: Server &lt;0.65.0&gt; received message {1211,11195,243031} from &lt;0.67.0&gt;</pre></div></div>
<p>Ah! It&#8217;s better, the message number is growing as expected. Notice that the threads are still the same (<code>&lt;0.65.0&gt;</code> for the server and <code>&lt;0.67.0&gt;</code> for the client).</p>
<p>There is no black magic here. The main part of the code is a loop. The server function is <code>server_loop</code> and it calls itself with <code>?MODULE:server_loop(…)</code>. The process is running inside the old version of the code, and when we call <code>server_loop</code> again, the new call is send to the new version of the code.</p>
<p>Be careful, this happens only because we call the function that way <code>?MODULE:server_loop</code> (<code>code_reload:server_loop</code> also works). Hot upgrade does not work if we call <code>server_loop</code> directly with going through the module again. This allows you to control where and when code reloading is done.</p>
<p>Now we can send a quit message to the client and the server:</p>
<div id="wpshdo_18" class="wp-synhighlighter-outer"><div id="wpshdt_18" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_18"></a><a id="wpshat_18" class="wp-synhighlighter-title" href="#codesyntax_18"  onClick="javascript:wpsh_toggleBlock(18)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_18" onClick="javascript:wpsh_code(18)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_18" onClick="javascript:wpsh_print(18)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.desgrange.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_18" class="wp-synhighlighter-inner" style="display: block;"><pre class="text" style="font-family:monospace;">5&gt; ClientPid ! {self(), quit}.
Received quit command from &lt;0.30.0&gt;
{&lt;0.30.0&gt;,quit}
6&gt; ServerPid ! {self(), quit}.
Received quit command from &lt;0.76.0&gt;
{&lt;0.76.0&gt;,quit}</pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.desgrange.net/2008/12/01/erlang-hot-swapping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

