方法抛出异常

异常处理

use std::io::ErrorKind;

//任何函数都可能引发 panic
fn method_with_panic() {
    panic!("test panic");
}

//任何方法都可以返回错误
fn method_with_error() -> Result<(), ErrorKind> { 
    Err(ErrorKind::NotFound) 
}

method_with_panic();

if let Err(err) = method_with_error() {
    println!("Error happened: {:?}", err);
}