docs

#E0063: 擲回的值不是 Error

只有實作了 Error 的型別才能被擲回。為該型別實作 Error,或擲回一個包裝該值的錯誤型別。

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(..);`

為型別加上 Debug 和 Display,並實作 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

相關錯誤碼:E0062 未處理的錯誤

發現錯誤? 在 GitHub 上編輯此頁 最後更新: