Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); var rand: std.Random = undefined; const Error = error{ Account_Is_Empty, Investment_Lost, }; const ErrorOrigin = enum { f2, f3, }; fn f1() !void { const n: u8 = rand.uintLessThan(u8, 9) + 1; if (n % 2 == 0) { return Error.Account_Is_Empty; } return Error.Investment_Lost; } fn f2() !void { try f1(); } fn f3() !void { try f1(); } fn f4(error_origin: ?*ErrorOrigin) !void { const n: u8 = rand.uintLessThan(u8, 9) + 1; if (n % 2 == 0) { f2() catch |err| { if (error_origin) |diagn| diagn.* = .f2; return err; }; } f3() catch |err| { if (error_origin) |diagn| diagn.* = .f2; return err; }; } const std_out = std.io.getStdOut().writer(); pub fn main() !void { var DefaultPrng = std.Random.DefaultPrng.init(@intCast(std.time.microTimestamp())); rand = DefaultPrng.random(); var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const alloc = gpa.allocator(); var error_origin: ErrorOrigin = undefined; f4(&error_origin) catch |err| { switch (error_origin) { .f2 => switch (err) { Error.Account_Is_Empty => try std_out.print("Aand it's gone\n", .{}), else => try std_out.print("This line is for bank members only\n", .{}), }, .f3 => switch (err) { Error.Investment_Lost => try std_out.print("The money in your account didn't do well\n", .{}), else => try std_out.print("This line is for bank members only\n", .{}), }, } try printTrace(err, alloc); }; try std_out.print("\n\n", .{}); } inline fn printTrace(err: Error, alloc: std.mem.Allocator) !void { const stack_trace: *std.builtin.StackTrace = @errorReturnTrace().?; const debug_info = try std.debug.getSelfDebugInfo(); const num_frames = @min(stack_trace.index, stack_trace.instruction_addresses.len); var i: usize = num_frames; while (i > 0) : (i -= 1) { const return_address = stack_trace.instruction_addresses[i - 1]; try std_out.print("{s} -> ", .{ (try (try debug_info.getModuleForAddress(return_address)).getSymbolAtAddress(alloc, return_address)).name, }); } try std_out.print("error.{s}", .{@errorName(err)}); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree