title: 17. 异常处理 tags: cairo starknet error assert panic WTF Cairo极简教程: 17. 异常处理 我最近在学 ,巩固一下细节,也写一个 ,供小白们使用。教程基于 版本。 推特:@0xAAScience|@WTFAcademy WTF Academy 社群:Discord|微信群|官网 wtf.academy 所有代码和教程开源在 github: github.com/WTFAcademy/WTF-Cairo 在本章中,我们将探索Cairo中的异常处理。通过这些技术,你将能够应对代码中潜在的异常,并通过异常消息提供反馈。 异常处理 Cairo提供了多种方法来处理代码中的异常。在本章中,我们将介绍其中的两种: (推荐使用)和 。
title: 17. 异常处理 tags: - cairo - starknet - error - assert - panic
我最近在学cairo-lang,巩固一下细节,也写一个WTF Cairo极简教程,供小白们使用。教程基于cairo 2.2.0版本。
WTF Academy 社群:Discord|微信群|官网 wtf.academy
所有代码和教程开源在 github: github.com/WTFAcademy/WTF-Cairo
在本章中,我们将探索Cairo中的异常处理。通过这些技术,你将能够应对代码中潜在的异常,并通过异常消息提供反馈。
Cairo提供了多种方法来处理代码中的异常。在本章中,我们将介绍其中的两种:assert()(推荐使用)和panic()。
在Cairo中,assert()函数是被推荐使用的异常处理方法。它的功能类似于Solidity中的require()函数,接受两个参数:
condition:条件,在程序运行时预期为true。error message:当condition为false时显示的异常消息(短字符串类型)。assert()会在运行时验证给定条件是否为true,如果不是,它将抛出一个异常消息。
在下面的例子中,如果input不等于0,程序将被中断,并显示异常消息'Error: Input not 0!'。
// 如果输入为0,则使用assert抛出错误(推荐) #[external(v0)] fn assert_example(self: @ContractState, input: u128){ assert( input == 0_u128, 'Error: Input not 0!'); }
panic()函数是Cairo提供的另一种异常处理方法。与assert()不同,panic()在不验证任何条件的情况下突然中止程序运行。它接受一个 felt252 数组作为参数(异常消息)。
我们可以改写assert_example()函数,使用panic()抛出异常:
use array::ArrayTrait; use traits::Into; // 如果输入为0,则用panic抛出异常 // panic()以felt252数组作为参数 #[external(v0)] fn panic_example(self: @ContractState, input: u128){ if input == 0_u128 { let mut error_data = ArrayTrait::new(); error_data.append(input.into()); panic(error_data); } }
此外,Cairo还提供了一个panic_with_felt252()函数。panic_with_felt252()和panic之间的唯一区别是panic_with_felt252()接受felt252作为参数,而不是数组。
让我们修改assert_example()以使用panic_with_felt252()抛出异常。
// 如果输入为0,则使用panic_with_felt252抛出异常 // panic_with_felt252()接受felt252作为参数 #[external(v0)] fn panic_with_felt252_example(self: @ContractState, input: u128){ if input == 0_u128 { panic_with_felt252('Error: Input not 0!'); }
为了提高代码的可用性,您可以编写一个模块专门存放错误的报错信息,在合约中使用use来调用该合约。
pub mod Errors { pub const NOT_ZERO: felt252 = 'Input not 0'; }
use super::Errors; #[external(v0)] fn panic_with_custom(self: @ContractState, input: u128){ if input != 0_u128 { panic_with_felt252(Errors::NOT_ZERO); } } #[external(v0)] fn assert_with_custom(self: @ContractState, input: u128){ assert(input == 0, Errors::NOT_ZERO); }
通过扩充Errors模块的错误处理,您可以方便地应对各种各样的错误,提高代码的可用性。
您可以在函数中加入nopanic修饰词来表明该函数中不会进行错误判断,如果存在,编译时会进行报错。
#[external(v0)] fn nopanic_example(self: @ContractState) -> felt252 nopanic{ 42 }
在本章中,我们探索了Cairo中可用于处理异常的各种技术。推荐的方法是使用assert()。它的运作方式类似于Solidity中的require(),验证条件,并在条件不满足时抛出异常消息,帮助确保你的代码按预期运行。