Trait std::error::Error1.0.0[][src]

pub trait Error: Debug + Display {
    fn source(&self) -> Option<&(dyn Error + 'static)> { ... }
fn backtrace(&self) -> Option<&Backtrace> { ... }
fn description(&self) -> &str { ... }
fn cause(&self) -> Option<&dyn Error> { ... } }
Expand description

Error 是 trait,表示对错误值 (即 Result<T, E> 中的 E 类型的值) 的基本期望。

错误必须通过 DisplayDebug traits 来描述自己。 错误消息通常是简洁的小写句子,没有尾随标点符号:

let err = "NaN".parse::<u32>().unwrap_err();
assert_eq!(err.to_string(), "invalid digit found in string");
Run

错误可能会提供原因链信息。Error::source() 通常在错误交叉 “abstraction boundaries” 时使用。 如果一个模块必须报告由下级模块的错误引起的错误,则它可以允许通过 Error::source() 访问该错误。 这使得高级模块可以提供自己的错误,同时还可以公开一些通过 source 链进行调试的实现。

Provided methods

此错误的下级来源 (如果有)。

Examples

use std::error::Error;
use std::fmt;

#[derive(Debug)]
struct SuperError {
    side: SuperErrorSideKick,
}

impl fmt::Display for SuperError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SuperError is here!")
    }
}

impl Error for SuperError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(&self.side)
    }
}

#[derive(Debug)]
struct SuperErrorSideKick;

impl fmt::Display for SuperErrorSideKick {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SuperErrorSideKick is here!")
    }
}

impl Error for SuperErrorSideKick {}

fn get_super_error() -> Result<(), SuperError> {
    Err(SuperError { side: SuperErrorSideKick })
}

fn main() {
    match get_super_error() {
        Err(e) => {
            println!("Error: {}", e);
            println!("Caused by: {}", e.source().unwrap());
        }
        _ => println!("No error"),
    }
}
Run
🔬 This is a nightly-only experimental API. (backtrace #53487)

返回发生错误的栈回溯 (如果有)。

该函数允许检查代码中发生错误的位置。 返回的 Backtrace 包含有关执行错误来源的 OS 线程的栈跟踪的信息。

请注意,并非所有错误都包含 Backtrace。另请注意,Backtrace 实际上可能为空。 有关更多信息,请查阅 Backtrace 类型本身。

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

if let Err(e) = "xc".parse::<u32>() {
    // 打印 `e` 本身,不需要 description()。
    eprintln!("Error: {}", e);
}
Run
👎 Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Implementations

如果 boxed 类型与 T 相同,则返回 true

如果 boxed 的类型为 T,则返回一些引用,如果不是,则返回 None

如果 boxed 的类型为 T,则返回一些可变引用; 如果不是,则返回 None

转发到在 dyn Error 类型上定义的方法。

转发到在 dyn Error 类型上定义的方法。

转发到在 dyn Error 类型上定义的方法。

转发到在 dyn Error 类型上定义的方法。

转发到在 dyn Error 类型上定义的方法。

转发到在 dyn Error 类型上定义的方法。

尝试将 box 转换为具体类型。

🔬 This is a nightly-only experimental API. (error_iter #58520)

返回一个迭代器,该迭代器从当前错误开始,然后以递归方式调用 Error::source

如果要忽略当前错误并仅使用其来源,请使用 skip(1)

Examples

#![feature(error_iter)]
use std::error::Error;
use std::fmt;

#[derive(Debug)]
struct A;

#[derive(Debug)]
struct B(Option<Box<dyn Error + 'static>>);

impl fmt::Display for A {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "A")
    }
}

impl fmt::Display for B {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "B")
    }
}

impl Error for A {}

impl Error for B {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.0.as_ref().map(|e| e.as_ref())
    }
}

let b = B(Some(Box::new(A)));

// 令 err: Box<Error> = b.into(); // 或者
let err = &b as &(dyn Error);

let mut iter = err.chain();

assert_eq!("B".to_string(), iter.next().unwrap().to_string());
assert_eq!("A".to_string(), iter.next().unwrap().to_string());
assert!(iter.next().is_none());
assert!(iter.next().is_none());
Run

尝试将 box 转换为具体类型。

尝试将 box 转换为具体类型。

Implementations on Foreign Types

Implementors

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

🔬 This is a nightly-only experimental API. (backtrace #53487)
👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

🔬 This is a nightly-only experimental API. (backtrace #53487)
👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

👎 Deprecated since 1.42.0:

use the Display impl or to_string()