mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2025-02-18 13:18:27 +00:00
167 lines
7.2 KiB
HTML
167 lines
7.2 KiB
HTML
<!DOCTYPE HTML>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>A Note About Error Handling - </title>
|
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
<meta name="description" content="">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
|
<base href="../">
|
|
|
|
<link rel="stylesheet" href="book.css">
|
|
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
|
|
|
|
<link rel="shortcut icon" href="favicon.png">
|
|
|
|
<!-- Font Awesome -->
|
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
|
|
|
|
<link rel="stylesheet" href="highlight.css">
|
|
<link rel="stylesheet" href="tomorrow-night.css">
|
|
|
|
<!-- MathJax -->
|
|
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
|
|
|
<!-- Fetch JQuery from CDN but have a local fallback -->
|
|
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
|
|
<script>
|
|
if (typeof jQuery == 'undefined') {
|
|
document.write(unescape("%3Cscript src='jquery.js'%3E%3C/script%3E"));
|
|
}
|
|
</script>
|
|
</head>
|
|
<body class="light">
|
|
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
<script type="text/javascript">
|
|
var theme = localStorage.getItem('theme');
|
|
if (theme == null) { theme = 'light'; }
|
|
$('body').removeClass().addClass(theme);
|
|
</script>
|
|
|
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|
<script type="text/javascript">
|
|
var sidebar = localStorage.getItem('sidebar');
|
|
if (sidebar === "hidden") { $("html").addClass("sidebar-hidden") }
|
|
else if (sidebar === "visible") { $("html").addClass("sidebar-visible") }
|
|
</script>
|
|
|
|
<div id="sidebar" class="sidebar">
|
|
<ul class="chapter"><li class="affix"><a href="pages/table_of_contents.html">Table of Contents</a></li><li class="affix"><a href="pages/error_handling_note.html" class="active">A Note About Error Handling</a></li><li><a href="pages/math.html"><strong>1.</strong> Math</a></li><li><ul class="section"><li><a href="pages/rand.html"><strong>1.1.</strong> rand</a></li></ul></li><li><a href="pages/algorithms.html"><strong>2.</strong> Algorithms</a></li><li><a href="pages/IO.html"><strong>3.</strong> IO</a></li><li><ul class="section"><li><a href="pages/file_io.html"><strong>3.1.</strong> File IO</a></li><li><a href="pages/cli_parsing.html"><strong>3.2.</strong> Command Line Parsing</a></li></ul></li><li><a href="pages/serialization.html"><strong>4.</strong> Serialization</a></li><li><ul class="section"><li><a href="pages/byteorder.html"><strong>4.1.</strong> Byteorder</a></li><li><a href="pages/json.html"><strong>4.2.</strong> JSON</a></li><li><a href="pages/toml.html"><strong>4.3.</strong> TOML</a></li></ul></li><li><a href="pages/networking.html"><strong>5.</strong> Networking</a></li><li class="affix"><a href="pages/contributing.html">Contributing</a></li></ul>
|
|
</div>
|
|
|
|
<div id="page-wrapper" class="page-wrapper">
|
|
|
|
<div class="page">
|
|
<div id="menu-bar" class="menu-bar">
|
|
<div class="left-buttons">
|
|
<i id="sidebar-toggle" class="fa fa-bars"></i>
|
|
<i id="theme-toggle" class="fa fa-paint-brush"></i>
|
|
</div>
|
|
|
|
<h1 class="menu-title"></h1>
|
|
|
|
<div class="right-buttons">
|
|
<i id="print-button" class="fa fa-print" title="Print this book"></i>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="content" class="content">
|
|
<a class="header" href="#a-note-about-error-handling" name="a-note-about-error-handling"><h1>A note about error handling</h1></a>
|
|
<p>Error handling in Rust is robust when done correctly, but in today's
|
|
Rust it requires a fair bit of boilerplate. Because of this one often
|
|
sees Rust examples filled with <code>unwrap</code> calls instead of proper error
|
|
handling.</p>
|
|
<p>Since these recipes are intended to be reused as-is and encourage best
|
|
practices, they set up error handling correctly, and when necessary to
|
|
reduce boilerplate, they use the [error-chain] crate.</p>
|
|
<p>The code for this setup generally looks like:</p>
|
|
<pre><code class="language-rust">#[macro_use]
|
|
extern crate error_chain;
|
|
|
|
mod errors {
|
|
error_chain! {
|
|
foreign_links {
|
|
Io(::std::io::Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
use errors::*;
|
|
|
|
fn main() { run().unwrap() }
|
|
|
|
fn run() -> Result<()> {
|
|
use std::io::Write;
|
|
let ref mut stdout = ::std::io::stdout();
|
|
writeln!(stdout, "hello, world")?;
|
|
|
|
Ok(())
|
|
}
|
|
</code></pre>
|
|
<p>This is using the <code>error_chain!</code> macro to define a custom <code>Error</code>
|
|
and <code>Result</code> type, along with an automatic conversion from
|
|
the common <code>::std::io::Error</code> type. The automatic conversion
|
|
makes the <code>?</code> operator work</p>
|
|
<p>For more background on error handling in Rust, read [this page of the Rust book][error-docs] and [this blog post][error-blog].</p>
|
|
|
|
</div>
|
|
|
|
<!-- Mobile navigation buttons -->
|
|
|
|
<a href="pages/table_of_contents.html" class="mobile-nav-chapters previous">
|
|
<i class="fa fa-angle-left"></i>
|
|
</a>
|
|
|
|
|
|
|
|
<a href="pages/math.html" class="mobile-nav-chapters next">
|
|
<i class="fa fa-angle-right"></i>
|
|
</a>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<a href="pages/table_of_contents.html" class="nav-chapters previous" title="You can navigate through the chapters using the arrow keys">
|
|
<i class="fa fa-angle-left"></i>
|
|
</a>
|
|
|
|
|
|
|
|
<a href="pages/math.html" class="nav-chapters next" title="You can navigate through the chapters using the arrow keys">
|
|
<i class="fa fa-angle-right"></i>
|
|
</a>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<!-- Local fallback for Font Awesome -->
|
|
<script>
|
|
if ($(".fa").css("font-family") !== "FontAwesome") {
|
|
$('<link rel="stylesheet" type="text/css" href="_FontAwesome/css/font-awesome.css">').prependTo('head');
|
|
}
|
|
</script>
|
|
|
|
<!-- Livereload script (if served using the cli tool) -->
|
|
|
|
<script type="text/javascript">
|
|
var socket = new WebSocket("ws://localhost:3001");
|
|
socket.onmessage = function (event) {
|
|
if (event.data === "reload") {
|
|
socket.close();
|
|
location.reload(true); // force reload from server (not from cache)
|
|
}
|
|
};
|
|
|
|
window.onbeforeunload = function() {
|
|
socket.close();
|
|
}
|
|
</script>
|
|
|
|
|
|
<script src="highlight.js"></script>
|
|
<script src="book.js"></script>
|
|
</body>
|
|
</html>
|