编译器异常

当编译合约时遇到问题,Vyper 会抛出一个或多个以下异常。

只要有可能,异常都会包含一个源代码高亮,显示代码中错误的位置。

vyper.exceptions.VariableDeclarationException: line 79:17 Persistent variable undeclared: highstBid
     78     # If bid is less than highest bid, bid fails
---> 79     if (value <= self.highstBid):
-------------------------^
     80         return False
exception ArgumentException

在使用无效参数调用函数时抛出,例如传递了错误数量的位置参数或无效的关键字参数。

exception CallViolation

在非法函数调用时抛出,例如尝试在两个外部函数之间进行调用。

exception ArrayIndexException

当数组索引越界时抛出。

exception EventDeclarationException

当事件声明无效时抛出。

exception EvmVersionException

当合约包含无法使用活动 EVM 规则集执行的操作时抛出。

exception FunctionDeclarationException

当函数声明无效时抛出,例如由于错误或不匹配的返回值。

exception ImmutableViolation

当尝试修改无法更改的变量、常量或定义时抛出。例如,尝试更新常量,或尝试将值赋值给函数定义。

exception InterfaceViolation

当接口未完全实现时抛出。

exception InvalidAttribute

在引用不存在的属性时抛出。

exception InvalidLiteral

当找不到给定字面值的有效类型时抛出。

@external
def foo():
    bar: decimal = 3.123456789123456789

此示例抛出 InvalidLiteral,因为给定的字面值小数位数过多,因此无法分配任何有效的 Vyper 类型。

exception InvalidOperation

当对给定类型使用无效操作符时抛出。

@external
def foo():
    a: String[10] = "hello" * 2

此示例抛出 InvalidOperation,因为字符串类型无法进行乘法运算。

exception InvalidReference

在引用现有定义时抛出无效引用。

baz: int128

@external
def foo():
    bar: int128 = baz

此示例抛出 InvalidReference,因为 baz 是一个存储变量。对其的引用应写为 self.baz

exception InvalidType

当对给定类型使用无效字面值时抛出。

@external
def foo():
    bar: int128 = 3.5

此示例抛出 InvalidType,因为 3.5 是一个有效的字面值,但无法转换为 int128

exception IteratorException

当迭代器构造或使用不正确时抛出。

exception JSONError

当编译器 JSON 输入格式错误时抛出。

exception NamespaceCollision

当尝试将变量赋值给已存在的名称时抛出。

exception NatSpecSyntaxException

当合约包含无效的 NatSpec 文档字符串时抛出。

vyper.exceptions.SyntaxException: line 14:5 No description given for tag '@param'
     13     @dev the feet are sticky like rice
---> 14     @param
-------------^
     15     @return always True
exception NonPayableViolation

当尝试从未标记为 @payable 的函数中访问 msg.value 时抛出。

@public
def _foo():
    bar: uint256 = msg.value
exception OverflowException

当数值超出给定类型的范围时抛出。

exception StateAccessViolation

当尝试在只读或无状态的上下文中执行修改操作时抛出。例如,在 @view 函数中写入存储,或在 @pure 函数中读取存储。

exception StructureException

在可解析但以某种方式无效的语法上抛出。

vyper.exceptions.StructureException: line 181:0 Invalid top-level statement
     180
---> 181 '''
---------^
     182
exception SyntaxException

在无法解析的无效语法上抛出。

vyper.exceptions.SyntaxException: line 4:20 invalid syntax
        3 struct Bid:
---> 4   blindedBid bytes32
---------------------------^
        5   deposit: uint256
exception TypeMismatch

当尝试对具有已知、不匹配类型的两个或多个对象执行操作时抛出。

@external
def foo(:
    bar: int128 = 3
    foo: decimal = 4.2

    if foo + bar > 4:
        pass

foo 的类型为 int128,而 bar 的类型为 decimal,因此尝试将它们加在一起会抛出 TypeMismatch

exception UndeclaredDefinition

当尝试访问未声明的对象时抛出。

exception VariableDeclarationException

在无效变量声明时抛出。

vyper.exceptions.VariableDeclarationException: line 79:17 Persistent variable undeclared: highstBid
     78     # If bid is less than highest bid, bid fails
---> 79     if (value <= self.highstBid):
-------------------------^
     80         return False
exception VersionException

当合约版本字符串格式错误或与当前编译器版本不兼容时抛出。

exception ZeroDivisionException

当出现除以零或模除零的情况时抛出。

编译器恐慌

exception CompilerPanic
$ vyper v.vy
Error compiling: v.vy
vyper.exceptions.CompilerPanic: Number of times repeated
must be a constant nonzero positive integer: 0 Please create an issue.

编译器恐慌错误表示编译器内部存在问题,应立即在 Vyper Github 页面上报告问题。如果您遇到此错误,请打开一个问题。请 打开一个问题