作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 error_chain
对于我的错误处理。
[dependencies]
error-chain = "0.12.2"
当我只使用单线程时,我没有问题。
#[macro_use] extern crate error_chain;
error_chain! {}
fn main() -> Result<()> {
crash_burn()
.chain_err(|| "crash_burn() crashed and burned!")?;
Ok(())
}
fn crash_burn() -> Result<()> {
bail!("I'm an error.")
}
但是,当尝试从线程返回错误时,出现编译错误。
#[macro_use] extern crate error_chain;
error_chain! {}
use std::thread::*;
fn main() -> Result<()> {
let child: JoinHandle<Result<()>> = spawn(move || {
crash_burn()
.chain_err(|| "crash_burn() crashed and burned!")?;
Ok(())
});
let res = child.join()
.chain_err(|| "Child thread panicked! This error did not come from crash_burn().")?;
res // res contains the error from crash_burn()
}
这是编译错误。
error[E0599]: no method named `chain_err` found for enum `std::result::Result<std::result::Result<(), Error>, std::boxed::Box<(dyn std::any::Any + std::marker::Send + 'static)>>` in the current scope
--> src/main.rs:13:10
|
13 | .chain_err(|| "Child thread panicked! This error did not come from crash_burn().")?;
| ^^^^^^^^^ method not found in `std::result::Result<std::result::Result<(), Error>, std::boxed::Box<(dyn std::any::Any + std::marker::Send + 'static)>>`
为什么不自动处理此错误?
最佳答案
Why is this error not handled automatically?
chain_err can be called on any Result type where the contained error type implements std::error::Error+Send + 'static
Box<dyn Any>
因为线程可能会因任何值而 panic 。
panic!(78);
但是
std::boxed::Box<dyn Any>
不实现错误特征(只有错误类型实现该特征)。
关于multithreading - 将 error_chain 与 JoinHandle 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62700780/
我用 error_chain 对于我的错误处理。 [dependencies] error-chain = "0.12.2" 当我只使用单线程时,我没有问题。 #[macro_use] extern
我有一个带有 error_chain 的 errors.rs 文件! {},它导出 Result、ResultExt、Error 和 ErrorKind。 如果我使用self::errors::*,I
我是一名优秀的程序员,十分优秀!