当前位置: 首页 > news >正文

【学Python自动化】 8.1 Python 与 Rust 错误处理对比学习笔记

一、语法错误 vs 编译错误

Python 语法错误


while True print('Hello world')
# SyntaxError: invalid syntax

Rust 编译错误


fn main() {while true println!("Hello world");// 错误:期望 `{`,找到 `println`
}

关键差异:

  • Python:运行时解析错误

  • Rust:编译时检查,更早发现错误

二、异常 vs Result 类型

Python 异常处理


# 自动抛出异常
10 * (1/0)  # ZeroDivisionError
4 + spam*3  # NameError
'2' + 2     # TypeError

Rust 错误处理


// 显式处理可能失败的操作
let result: Result<i32, &str> = Ok(42);
let error: Result<i32, &str> = Err("Something went wrong");// 必须处理 Result 类型
match result {Ok(value) => println!("Value: {}", value),Err(e) => println!("Error: {}", e),
}

三、异常处理机制对比

Python try-except


while True:try:x = int(input("Please enter a number: "))breakexcept ValueError:print("Oops! That was no valid number. Try again...")except (RuntimeError, TypeError, NameError) as e:print(f"Other error: {e}")else:print("No exception occurred")finally:print("This always runs")

Rust Result 处理


use std::io;fn get_number() -> Result<i32, io::Error> {let mut input = String::new();loop {println!("Please enter a number: ");io::stdin().read_line(&mut input)?;match input.trim().parse() {Ok(num) => return Ok(num),Err(_) => {println!("Oops! That was no valid number. Try again...");input.clear();continue;}}}
}// 使用 ? 操作符传播错误
fn process() -> Result<(), io::Error> {let x = get_number()?;  // 自动传播错误println!("You entered: {}", x);Ok(())
}

四、抛出/引发异常对比

Python raise


def validate_age(age):if age < 0:raise ValueError("Age cannot be negative")return agetry:validate_age(-1)
except ValueError as e:print(f"Caught exception: {e}")

Rust Result 返回


fn validate_age(age: i32) -> Result<i32, &'static str> {if age < 0 {return Err("Age cannot be negative");}Ok(age)
}match validate_age(-1) {Ok(age) => println!("Valid age: {}", age),Err(e) => println!("Error: {}", e),
}

五、异常链 vs 错误转换

Python 异常链


try:open("database.sqlite")
except OSError as e:raise RuntimeError("Unable to handle error") from e

Rust 错误转换


use std::fs::File;
use std::io;fn open_database() -> Result<File, io::Error> {File::open("database.sqlite").map_err(|e| io::Error::new(io::ErrorKind::Other, "Unable to handle error"))
}

六、自定义异常/错误

Python 自定义异常


class MyError(Exception):def __init__(self, message, code):super().__init__(message)self.code = codetry:raise MyError("Something went wrong", 500)
except MyError as e:print(f"Error {e.code}: {e}")

Rust 自定义错误


use thiserror::Error;#[derive(Error, Debug)]
enum MyError {#[error("Something went wrong with code {0}")]WithCode(i32),#[error("IO error: {0}")]Io(#[from] std::io::Error),
}fn might_fail() -> Result<(), MyError> {if condition {Err(MyError::WithCode(500))} else {Ok(())}
}

七、清理操作对比

Python finally


try:f = open("file.txt")# 处理文件
finally:f.close()  # 总是执行

Rust Drop trait


struct FileWrapper {file: File,
}impl Drop for FileWrapper {fn drop(&mut self) {// 自动清理,类似 finally// 文件会在离开作用域时自动关闭}
}{let file_wrapper = FileWrapper { file: File::open("file.txt")? };// 使用文件
} // 这里自动调用 drop()

八、资源管理对比

Python with 语句


with open("myfile.txt") as f:for line in f:print(line, end="")
# 文件自动关闭

Rust RAII 模式


use std::fs::File;
use std::io::{BufRead, BufReader};{let file = File::open("myfile.txt")?;let reader = BufReader::new(file);for line in reader.lines() {println!("{}", line?);}
} // 文件自动关闭

九、批量错误处理对比

Python ExceptionGroup


excs = []
for test in tests:try:test.run()except Exception as e:excs.append(e)if excs:raise ExceptionGroup("Test Failures", excs)

Rust 错误收集


use anyhow::Result;let mut errors = Vec::new();
for test in tests {if let Err(e) = test.run() {errors.push(e);}
}if !errors.is_empty() {// 处理多个错误
}

十、错误注释/上下文

Python add_note()


try:raise TypeError('bad type')
except Exception as e:e.add_note('Additional context')raise

Rust 错误上下文


use anyhow::{Context, Result};fn process_file() -> Result<()> {let content = std::fs::read_to_string("file.txt").context("Failed to read file")?;Ok(())
}

十一、🔑 核心哲学差异

特性PythonRust
错误处理异常机制Result/Option 类型
错误发现运行时编译时
强制处理可选必须处理 Result
性能异常堆栈跟踪有开销零成本抽象
代码流程控制流跳转显式处理每个可能错误
资源清理finally 或 withRAII 和 Drop

十二、💡 最佳实践

Python 异常处理建议

  • 具体异常:捕获具体异常而不是通用的 Exception

  • 异常信息:提供清晰的错误信息

  • 资源清理:使用 with 语句管理资源

  • 异常链:使用 from 保留原始异常信息

Rust 错误处理建议

  • 显式处理:使用 match 或 ? 操作符处理所有错误

  • 错误类型:定义清晰的错误类型

  • 错误转换:使用 map_err 或 库如 anyhow/thiserror

  • 尽早返回:使用 ? 操作符传播错误

十三、🎯 转换思维

从 Python 到 Rust

  • 忘记 try-except,学习 match 和 Result

  • 资源管理从 with 转到 RAII

  • 错误信息从异常消息转到枚举变体

从 Rust 到 Python

  • 享受异常处理的简洁性

  • 但要注意捕获过于宽泛的异常

  • 利用 with 语句简化资源管理

两种语言都有优秀的错误处理机制,Python 更注重开发效率,Rust 更注重安全性和性能!


文章转载自:

http://8BHzdQmS.fkgqn.cn
http://QyZjy3BW.fkgqn.cn
http://uVjXaoUZ.fkgqn.cn
http://DYmPXhzE.fkgqn.cn
http://2Ou7KwZm.fkgqn.cn
http://eqGrafSN.fkgqn.cn
http://ubloemxQ.fkgqn.cn
http://R645XfOX.fkgqn.cn
http://L9GW6yz2.fkgqn.cn
http://O5YaA2pO.fkgqn.cn
http://vOMg13ec.fkgqn.cn
http://y4adUyhT.fkgqn.cn
http://8eoerMpU.fkgqn.cn
http://jqIPrX46.fkgqn.cn
http://URiSJJTm.fkgqn.cn
http://avIZucDk.fkgqn.cn
http://jEnhjJOa.fkgqn.cn
http://ZBj7n7e1.fkgqn.cn
http://eXya65lQ.fkgqn.cn
http://RqZ7HnBm.fkgqn.cn
http://2cH7rALd.fkgqn.cn
http://roKpFqa0.fkgqn.cn
http://nKV9USWR.fkgqn.cn
http://Wg3xmYwM.fkgqn.cn
http://epQv4ZcZ.fkgqn.cn
http://egyFGBIO.fkgqn.cn
http://FHUi05Z6.fkgqn.cn
http://nWBnMuTc.fkgqn.cn
http://vqDBSEYi.fkgqn.cn
http://GjbVzeSU.fkgqn.cn
http://www.dtcms.com/a/362544.html

相关文章:

  • 拔河(蓝桥杯)(前缀和)
  • Docker CI/CD 自动化部署配置指南
  • 【Datawhale之Happy-LLM】3种常见的decoder-only模型——Github最火大模型原理与实践教程task07
  • C#---共享项目
  • 【C++变量和数据类型:从基础到高级】
  • AI 在教育领域的落地困境:个性化教学与数据隐私的平衡之道
  • 线程特定存储
  • 【Go语言入门教程】 Go语言的起源与技术特点:从诞生到现代编程利器(一)
  • 深入浅出 RabbitMQ-TTL+死信队列+延迟队列
  • idea上传本地项目代码到Gitee仓库教程
  • 【论文阅读】Deepseek-VL:走向现实世界的视觉语言理解
  • 【Web前端】JS+DOM来实现乌龟追兔子小游戏
  • GPT-5在医疗领域应用的研究效能初探(下)
  • 跨平台游戏引擎 Axmol-2.8.0 发布
  • https_server.cpython-310.pyc 等pyc后缀这些是什么文件
  • Python+DRVT 从外部调用 Revit:批量创建墙
  • DVWA靶场通关笔记-反射型XSS(Impossible级别)
  • 4.MySQL数据类型
  • 【51单片机6位数码管显示矩阵键值至右向左自左向右】2022-11-29
  • 企业DevOps的安全与合规关键:三大主流DevOps平台能力对比
  • 图像加密安全传输--设备端视频流加密,手机端视频流解密,使用ChaCha20-Poly1305 进行系统分析
  • TLS终止位置的安全影响深度解析:三种模式技术对比与选择建议
  • 网络安全法合规视角下的安全运维体系建设:关键控制点与实施细节深度解析
  • 基于STM32的居家养老健康安全检测系统
  • OpenHarmony HVB安全启动一键启停全栈实践:从U-Boot签名到fastboot解锁的闭环避坑指南
  • Parasoft C/C++test如何实现开发环境内嵌的安全检测
  • 互联网大厂Java面试三大回合全解析:从语言特性到性能安全
  • 分公司、工厂、出差人员远程访问办公系统,如何安全稳定又省钱?
  • 【数据结构】八大排序之快速排序:分而治之的艺术
  • BeaGo-李开复旗下公司推出的AI搜索助手