blob: cbc9f48033c7651d09b33323d5e367214da87b81 [file] [log] [blame]
<!DOCTYPE html>
<html class="no-js">
<head lang="en-us">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<title>Exceptions - Eclipse Mita</title>
<meta name="generator" content="Hugo 0.40.1" />
<meta name="description" content="The documentation of Eclipse Mita.">
<link rel="canonical" href="../../language/exceptions/">
<meta property="og:url" content="/language/exceptions/">
<meta property="og:title" content="Eclipse Mita">
<meta property="og:image" content="/images/mita.svg">
<meta name="apple-mobile-web-app-title" content="Eclipse Mita">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<link rel="shortcut icon" type="image/x-icon" href="../../images/favicon.ico">
<link rel="icon" type="image/x-icon" href="../../images/favicon.ico">
<style>
@font-face {
font-family: 'Icon';
src: url('/fonts/icon.eot');
src: url('/fonts/icon.eot')
format('embedded-opentype'),
url('/fonts/icon.woff')
format('woff'),
url('/fonts/icon.ttf')
format('truetype'),
url('/fonts/icon.svg')
format('svg');
font-weight: normal;
font-style: normal;
}
</style>
<link rel="stylesheet" href="../../stylesheets/application.css">
<link rel="stylesheet" href="../../stylesheets/temporary.css">
<link rel="stylesheet" href="../../stylesheets/palettes.css">
<link rel="stylesheet" href="../../stylesheets/highlight/highlight.css">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Ubuntu:400,700|Ubuntu&#43;Mono">
<style>
body, input {
font-family: 'Ubuntu', Helvetica, Arial, sans-serif;
}
pre, code {
font-family: 'Ubuntu Mono', 'Courier New', 'Courier', monospace;
}
</style>
<link rel="stylesheet" href="../../css/custom.css">
<script src="../../javascripts/modernizr.js"></script>
</head>
<body class="palette-primary-blue-grey palette-accent-light-blue">
<div class="backdrop">
<div class="backdrop-paper"></div>
</div>
<input class="toggle" type="checkbox" id="toggle-drawer">
<input class="toggle" type="checkbox" id="toggle-search">
<label class="toggle-button overlay" for="toggle-drawer"></label>
<header class="header">
<nav aria-label="Header">
<div class="bar default">
<div class="button button-menu" role="button" aria-label="Menu">
<label class="toggle-button icon icon-menu" for="toggle-drawer">
<span></span>
</label>
</div>
<div class="stretch">
<div class="title">
Exceptions
</div>
</div>
<div class="button button-twitter" role="button" aria-label="Twitter">
<a href="https://twitter.com/eclipse_mita" title="@eclipse_mita on Twitter" target="_blank" class="toggle-button icon icon-twitter"></a>
</div>
<div class="button button-github" role="button" aria-label="GitHub">
<a href="https://github.com/eclipse/mita" title="@eclipse/mita on GitHub" target="_blank" class="toggle-button icon icon-github"></a>
</div>
</div>
<div class="bar search">
<div class="button button-close" role="button" aria-label="Close">
<label class="toggle-button icon icon-back" for="toggle-search"></label>
</div>
<div class="stretch">
<div class="field">
<input class="query" type="text" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck>
</div>
</div>
<div class="button button-reset" role="button" aria-label="Search">
<button class="toggle-button icon icon-close" id="reset-search"></button>
</div>
</div>
</nav>
</header>
<main class="main">
<div class="drawer">
<nav aria-label="Navigation">
<a href="../../" class="project">
<div class="banner">
<div class="logo">
<img src="../../images/mita.svg">
</div>
<div class="name">
<strong>Eclipse Mita </strong>
</div>
</div>
</a>
<div class="scrollable">
<div class="wrapper">
<div class="toc">
<ul>
<li>
<a title="Download" href="http://github.com/eclipse/mita">
Download
</a>
</li>
<li>
<a title="Concepts" href="../../concepts/">
Concepts
</a>
</li>
<li>
<span class="section">Language</span>
<ul>
<a title="Introduction" href="../../language/introduction/">
Introduction
</a>
<a title="Basics" href="../../language/basics/">
Basics
</a>
<a title="Packages" href="../../language/packages/">
Packages
</a>
<a title="System Setup" href="../../language/setup/">
System Setup
</a>
<a title="Types" href="../../language/types/">
Types
</a>
<a title="Arrays" href="../../language/arrays/">
Arrays
</a>
<a title="Functions" href="../../language/functions/">
Functions
</a>
<a title="Foreign Function Interface" href="../../language/foreignfunctioninterface/">
Foreign Function Interface
</a>
<a title="Events" href="../../language/events/">
Events
</a>
<a class="current" title="Exceptions" href="../../language/exceptions/">
Exceptions
</a>
<ul id="scrollspy">
</ul>
</ul>
</li>
<li>
<span class="section">Platforms</span>
<ul>
<a title="Arduino Uno" href="../../platforms/arduinouno/">
Arduino Uno
</a>
<a title="XDK110" href="../../platforms/xdk110/">
XDK110
</a>
</ul>
</li>
</ul>
</div>
</div>
</div>
</nav>
</div>
<article class="article">
<div class="wrapper">
<h1>Exceptions </h1>
<h2 id="exceptions">Exceptions</h2>
<p>Exceptions are special types defined by the exception keyword:</p>
<div class="highlight"><pre class="chroma"><code class="language-TypeScript" data-lang="TypeScript"><span class="nx">exception</span> <span class="nx">FooException</span><span class="p">;</span></code></pre></div>
<p>Exceptions can be thrown, which returns control flow to the caller. If the exception is not caught by the caller it propagates further. If the exception is never caught it causes a system reset.</p>
<div class="highlight"><pre class="chroma"><code class="language-TypeScript" data-lang="TypeScript"><span class="k">throw</span> <span class="nx">FooException</span><span class="p">;</span></code></pre></div>
<p>Exceptions can be caught with the familiar <code>try...catch</code> syntax:</p>
<div class="highlight"><pre class="chroma"><code class="language-TypeScript" data-lang="TypeScript"><span class="k">try</span> <span class="p">{</span>
<span class="k">throw</span> <span class="nx">FooException</span><span class="p">;</span>
<span class="p">}</span> <span class="k">catch</span><span class="p">(</span><span class="nx">FooException</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">println</span><span class="p">(</span><span class="s2">&#34;Caught FooException&#34;</span><span class="p">);</span>
<span class="p">}</span></code></pre></div>
<p>Exceptions are implicit, meaning that functions do not have to (and cannot) declare the exceptions they might throw.</p>
<div class="highlight"><pre class="chroma"><code class="language-TypeScript" data-lang="TypeScript"><span class="nx">fn</span> <span class="nx">doSomething() {</span>
<span class="k">throw</span> <span class="nx">FooException</span><span class="p">;</span>
<span class="p">}</span></code></pre></div>
<p>Similarly to other languages you can also have a <code>finally</code> block for cleanup:</p>
<div class="highlight"><pre class="chroma"><code class="language-TypeScript" data-lang="TypeScript"><span class="k">try</span> <span class="p">{</span>
<span class="k">throw</span> <span class="nx">FooException</span><span class="p">;</span>
<span class="p">}</span> <span class="k">catch</span><span class="p">(</span><span class="nx">FooException</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">println</span><span class="p">(</span><span class="s2">&#34;Caught FooException&#34;</span><span class="p">);</span>
<span class="p">}</span> <span class="k">finally</span> <span class="p">{</span>
<span class="nx">println</span><span class="p">(</span><span class="s2">&#34;In finally&#34;</span><span class="p">);</span>
<span class="p">}</span>
<span class="cm">/* prints:
</span><span class="cm">Caught FooException
</span><span class="cm">In finally
</span><span class="cm">*/</span></code></pre></div>
<p>If you want to have a &ldquo;catch all&rdquo;, you can catch just <code>Exception</code>, which is a kind-of &ldquo;supertype&rdquo; of all other exceptions:</p>
<pre><code>TypeScript
try {
throw FooException;
} catch(Exception) {
println(&quot;Caught something&quot;);
}
</code></pre>
<p>You can nest <code>try/catch/finally</code> within each other freely.</p>
<aside class="copyright" role="note">
&copy; 2018 The Eclipse Mita Project &ndash;
Documentation built with
<a href="https://www.gohugo.io" target="_blank">Hugo</a>
using the
<a href="http://github.com/digitalcraftsman/hugo-material-docs" target="_blank">Material</a> theme.
</aside>
<footer class="footer">
<nav class="pagination" aria-label="Footer">
<div class="previous">
<a href="../../platforms/arduinouno/" title="Arduino Uno">
<span class="direction">
Previous
</span>
<div class="page">
<div class="button button-previous" role="button" aria-label="Previous">
<i class="icon icon-back"></i>
</div>
<div class="stretch">
<div class="title">
Arduino Uno
</div>
</div>
</div>
</a>
</div>
<div class="next">
<a href="../../download/" title="Download">
<span class="direction">
Next
</span>
<div class="page">
<div class="stretch">
<div class="title">
Download
</div>
</div>
<div class="button button-next" role="button" aria-label="Next">
<i class="icon icon-forward"></i>
</div>
</div>
</a>
</div>
</nav>
</footer>
</div>
</article>
<div class="results" role="status" aria-live="polite">
<div class="scrollable">
<div class="wrapper">
<div class="meta"></div>
<div class="list"></div>
</div>
</div>
</div>
</main>
<script>
var base_url = '';
var repo_id = '';
</script>
<script src="../../javascripts/application.js"></script>
<script>
/* Add headers to scrollspy */
var headers = document.getElementsByTagName("h2");
var scrollspy = document.getElementById('scrollspy');
if(scrollspy) {
if(headers.length > 0) {
for(var i = 0; i < headers.length; i++) {
var li = document.createElement("li");
li.setAttribute("class", "anchor");
var a = document.createElement("a");
a.setAttribute("href", "#" + headers[i].id);
a.setAttribute("title", headers[i].innerHTML);
a.innerHTML = headers[i].innerHTML;
li.appendChild(a)
scrollspy.appendChild(li);
}
} else {
scrollspy.parentElement.removeChild(scrollspy)
}
/* Add permanent link next to the headers */
var headers = document.querySelectorAll("h1, h2, h3, h4, h5, h6");
for(var i = 0; i < headers.length; i++) {
var a = document.createElement("a");
a.setAttribute("class", "headerlink");
a.setAttribute("href", "#" + headers[i].id);
a.setAttribute("title", "Permanent link")
a.innerHTML = "#";
headers[i].appendChild(a);
}
}
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.8.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</body>
</html>