Him*_*jal 3 struct segmentation-fault zig
如何try-catch在 Zig 中实现经典的错误处理?
例如。如何解决这个错误并且只有append在没有错误发生时才执行?
var stmt = self.statement() catch {
self.synchronize(); // Only execute this when there is an error.
};
self.top_level.statements.append(stmt); // HELP? This should only be executed when no error
// ...
fn synchronize() void {
// ...implementation
}
fn statement() SomeError!void {
// ...implementation
}
Run Code Online (Sandbox Code Playgroud)
如果可能,请显示上述代码的修改版本。
尝试使用 if-else,如下所示:
if (self.statement()) |stmt| {
// HELP? This should only be executed when no error
self.top_level.statements.append(stmt)
} else |error| {
// Only execute this when there is an error.
self.synchronize()
}
Run Code Online (Sandbox Code Playgroud)
您可以在 Zig 文档中了解有关 if 的更多信息