docs

你正在阅读 v0.1.0 的文档。Nabi 0.1.0 已不再提供支持。 最新版本:v0.1.3 →

本页尚未翻译,以下为英文原文。

#E0063: thrown value is not an Error

Only types that implement Error can be thrown. Implement Error for the type, or throw an error type that wraps the value.

nabi
struct Plain { msg: str; }

fn f() throws {
    throw Plain { msg: "bad" };
}
error[E0063]: `Plain` can't be thrown: it does not implement `Error`
  --> main.nabi:4:5
   |
 4 |     throw Plain { msg: "bad" };
   |     ^^^^^
   |
   = help: add `impl Error for Plain {}` (it needs `Display` and `Debug`), or throw a `ValueError`: `throw ValueError::new(..);`

Give the type Debug and Display, and implement Error:

nabi
#[derive(Debug)]
struct Plain { msg: str; }
impl Display for Plain {
    fn fmt(self, ref f: Formatter) throws (FmtError) { try f.write_str(self.msg); }
}
impl Error for Plain {}

fn f() throws {
    throw Plain { msg: "bad" };
}
try f() catch e { println(f"{e}") };
bad

Related: E0062 error not handled

Found a mistake? Edit this page on GitHub. Last updated