docs

您正在閱讀 v0.1.2 的文件。Nabi 0.1.2 已停止支援。 最新版本: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