#E0062: 未處理的錯誤
呼叫可能擲回錯誤,而這個錯誤既沒有被攔截,也沒有被宣告。用 try 標記呼叫,然後攔截錯誤,或把它加到外圍函式的 throws 中。
nabi
let n = "42".parse::<i32>();
error[E0062]: this call can throw `ParseError` and must be marked with `try` (`try f()`; `try? f()` / `try! f()` to drop / panic on the error) --> main.nabi:1:9 | 1 | let n = "42".parse::<i32>(); | ^^^^^^^^^^^^^^^^^^^
用 try 標記呼叫,並在錯誤繼續往外傳的地方宣告 throws:
nabi
fn parse_n(s: str) throws -> i32 {
try s.parse::<i32>()
}
println(f"{try parse_n("42")}");
42
try? f() 會把錯誤轉成 none,try! f() 則在出錯時觸發 panic(第 10 章)。
相關錯誤碼:E0063 擲回的值不是 Error、E0064 對非可選的值使用可選運算子、E0096 沒有作用的 try 或 catch
發現錯誤? 在 GitHub 上編輯此頁 最後更新: