Cairo 102 进阶

1. 事件
2. 异常处理
3. 所有权-作用域
4. 所有权-Move
5. 所有权 III 保留所有权
6. 所有权 IV 引用
7. 所有权 V 快照
8. 特质和实现
9. 泛型
10. 接口/ABI
17. 异常处理
异常处理

在本章中,我们将探索Cairo中的异常处理。通过这些技术,你将能够应对代码中潜在的异常,并通过异常消息提供反馈。

异常处理

Cairo提供了多种方法来处理代码中的异常。在本章中,我们将介绍其中的两种:assert()(推荐使用)和panic()。

Assert

在Cairo中,assert()函数是被推荐使用的异常处理方法。它的功能类似于Solidity中的require()函数,接受两个参数:

  1. condition:条件,在程序运行时预期为true。
  2. 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

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!');
    }

总结

在本章中,我们探索了Cairo中可用于处理异常的各种技术。推荐的方法是使用assert()。它的运作方式类似于Solidity中的require(),验证条件,并在条件不满足时抛出异常消息,帮助确保你的代码按预期运行。

PreviousNext