Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

zig Error Handling

zig Error Handling

Handling errors in Zig is a common task, especially when doing system-level programming.

Zig provides a flexible and explicit error handling mechanism that enables developers to clearly manage and handle errors.

Zig uses an explicit error handling mechanism, handling errors through the ! symbol and the try statement.

Error handling in Zig is not implicit like exceptions; instead, it is explicitly represented in the code, making error handling more transparent and controllable.

The following are some basic error handling strategies and techniques:


Error types

In Zig, the error type is usually represented by the ! symbol, which is a generic type representing a value that may produce an error.

Example

const

std

=

@import

(

"std"

)

;

pub fn mightFail

(

)

!

void

{

   

return

error.

SomeError

;

}


Error Handling Mechanism

1. Using the try Statement

The try statement is used to automatically handle errors in function calls. If the function returns an error, try causes the outer function to immediately return that error.

Example

const

std

=

@import

(

"std"

)

;

// Define a function that may fail

pub fn mightFail

(

)

!

void

{

   

return

error.

SomeError

;

// Return an example error

}

// Allow the main function to return an error

pub fn main

(

)

!

void

{

   

// Attempt to call the mightFail function; if it fails, main also returns that error

   

try

mightFail

(

)

;

   

// If there is no error, continue execution

    std.

debug

.

print

(

"Success!\n"

, .

{

}

)

;

}

2. Using the catch Statement

The catch statement is used to catch errors and provide handling logic. If the function call fails, the catch statement can execute specific error handling code.

Example

const

std

=

@import

(

"std"

)

;

pub fn mightFail

(

)

!

void

{

   

return

error.

SomeError

;

}

pub fn main

(

)

void

{

   

// Handle the error directly to avoid unused variables

    _

=

mightFail

(

)

catch

|

err

|

{

        std.

debug

.

print

(

"Error occurred: {}\n"

, .

{

err

}

)

;

       

return

;

// Exit after handling the error

   

}

;

    std.

debug

.

print

(

"Success!\n"

, .

{

}

)

;

}

The compilation and execution result is:

Error occurred: error.SomeError

3. Using catch to Capture Specific Errors

The catch statement can capture and handle specific error types, and errors caught via catch can be handled in more detail.

Example

const

std

=

@import

(

"std"

)

;

const

Error

=

error

{

    NotFound,

    PermissionDenied,

}

;

pub fn mightFail

(

)

!

void

{

   

return

Error.

NotFound

;

// Return an example error

}

pub fn main

(

)

void

{

   

// Handle the error directly without storing the result in a variable

    _

=

mightFail

(

)

catch

|

err

|

{

       

switch

(

err

)

{

            Error.

NotFound

=>

std.

debug

.

print

(

"Not found error occurred\n"

, .

{

}

)

,

            Error.

PermissionDenied

=>

std.

debug

.

print

(

"Permission denied error occurred\n"

, .

{

}

)

,

       

}

       

return

;

// Exit after handling the error

   

}

;

    std.

debug

.

print

(

"Success!\n"

, .

{

}

)

;

// If there is no error, continue execution

}

<

h3

The compilation and execution result is:

Not found error occurred

  1. Using the defer Statement

The defer statement is used to perform cleanup operations when a function exits, whether it returns normally or due to an error. It is similar to the finally statement in other programming languages.

Example

const

std

=

@import

(

"std"

)

;

pub fn someFunction

(

)

void

{

    defer std.

debug

.

print

(

"Cleanup code executed\n"

, .

{

}

)

;

   

// Function logic

    std.

debug

.

print

(

"Function logic\n"

, .

{

}

)

;

   

// An error may occur here, but the defer code will still execute

}

pub fn main

(

)

void

{

    someFunction

(

)

;

}

The compilation and execution result is:

Function logic

Cleanup code executed