Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Exceptions are just a specific example of a more general case of advanced non-local flow control constructs. Other examples are:</p> <ul> <li><em>notifications</em> (a generalization of exceptions, originally from some old Lisp object system, now implemented in e.g. CommonLisp and Ioke), </li> <li><em>continuations</em> (a more structured form of <code>GOTO</code>, popular in high-level, higher-order languages), </li> <li><em>coroutines</em> (a generalization of subroutines, popular especially in Lua), </li> <li><em>generators</em> à la Python (essentially a restricted form of coroutines), </li> <li><em>fibers</em> (cooperative light-weight threads) and of course the already mentioned </li> <li><code>GOTO</code>.</li> </ul> <p>(I'm sure there's many others I missed.)</p> <p>An interesting property of these constructs is that they are all roughly equivalent in expressive power: if you have <em>one</em>, you can pretty easily build all the others.</p> <p>So, how you best implement exceptions depends on what other constructs you have available: </p> <ul> <li>Every CPU has <code>GOTO</code>, therefore you can always fall back to that, if you must.</li> <li>C has <code>setjmp</code>/<code>longjmp</code> which are basically MacGyver continuations (built out of duct-tape and toothpicks, not quite the real thing, but will at least get you out of the immediate trouble if you don't have something better available). </li> <li>The JVM and CLI have exceptions of their own, which means that if the exception semantics of your language match Java's/C#'s, you are home free (but if not, then you are screwed). </li> <li>The Parrot VM as both exceptions and continuations. </li> <li>Windows has its own framework for exception handling, which language implementors can use to build their own exceptions on top.</li> </ul> <p>A very interesting use case, both of the <em>usage</em> of exceptions <em>and</em> the <em>implementation</em> of exceptions is Microsoft Live Lab's Volta Project. (Now defunct.) The goal of Volta was to provide architectural refactoring for Web applications at the push of a button. So, you could turn your one-tier web application into a two- or three-tier application just by putting some <code>[Browser]</code> or <code>[DB]</code> attributes on your .NET code and the code would then automagically run on the client or in the DB. In order to do that, the .NET code had to be translated to JavaScript source code, obviously.</p> <p>Now, you <em>could</em> just write an entire VM in JavaScript and run the bytecode unmodified. (Basically, port the CLR from C++ to JavaScript.) There are actually projects that do this (e.g. the HotRuby VM), but this is both inefficient and not very interoperable with other JavaScript code.</p> <p>So, instead, they wrote a compiler which compiles CIL bytecode to JavaScript sourcecode. However, JavaScript lacks certain features that .NET has (generators, threads, also the two exception models aren't 100% compatible), and more importantly it lacks certain features that compiler writers <em>love</em> (either <code>GOTO</code> or continuations) and that could be used to implement the above-mentioned missing features.</p> <p>However, JavaScript <em>does</em> have exceptions. So, they used <em>JavaScript Exceptions</em> to implement <em>Volta Continuations</em> and then they used <em>Volta Continuations</em> to implement <em>.NET Exceptions</em>, <em>.NET Generators</em> and even <em>.NET Managed Threads</em>(!!!)</p> <p>So, to answer your original question:</p> <blockquote> <p>How are exceptions implemented under the hood?</p> </blockquote> <p>With Exceptions, ironically! At least in this very specific case, anyway.</p> <p>Another great example is some of the exception proposals on the Go mailing list, which implement exceptions using Goroutines (something like a mixture of concurrent coroutines ans CSP processes). Yet another example is Haskell, which uses Monads, lazy evaluation, tail call optimization and higher-order functions to implement exceptions. Some modern CPUs also support basic building blocks for exceptions (for example the Vega-3 CPUs that were specifically designed for the Azul Systems Java Compute Accelerators).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload