1. Write a function parseUpper that accepts a byte and returns the uppercase letter value. Return error.InvalidUppercase if the byte is not between 'A' and 'Z'.
- Write a function
parseUpperthat accepts a byte and returns the uppercase letter value. Returnerror.InvalidUppercaseif the byte is not between'A'and'Z'.
fn parseUpper(c: u8) !u8 {
// ...
}- Write a function
safeDividethat divides two integers. Returnerror.DivisionByZerowhen the divisor is zero.
fn safeDivide(a: u32, b: u32) !u32 {
// ...
}- Write a function
firstthat returns the first byte of a slice. Returnerror.EmptySliceif the slice is empty.
fn first(s: []const u8) !u8 {
// ...
}Write a function
lastthat returns the last byte of a slice.Write a function
requirePositivethat returnserror.NegativeNumberif its argument is less than zero.Rewrite this code using
try:
const value = parseNumber(text) catch |err| return err;- Rewrite this code using
catch:
const value = try parseNumber(text);- Write a function
parseTwoDigitsthat parses two ASCII digits into a number between0and99.
try parseTwoDigits('4', '2') // 42Write a function that opens a file and closes it with
defer.Allocate memory with an allocator and release it with
defer.Allocate memory and return it from a function. Use
errdeferso that memory is freed only on failure.Write a function that translates
error.FileNotFoundintoerror.MissingConfig.Write a program that reads a filename from the command line and prints a clear error message if opening the file fails.
Write a parser that never prints errors directly. Handle all printing in
main.Write a function that parses an integer from a byte slice and returns:
| Error | Meaning |
|---|---|
error.EmptyInput | slice is empty |
error.InvalidDigit | non-digit found |
error.Overflow | value does not fit |
- Write a function that reads a file into memory using:
readFileAllocdefertrycatch
Write a function that creates two resources and releases them in reverse order using multiple
deferstatements.Modify a previous program so that all resource cleanup is handled by
deferand all error propagation usestry.Write a small command-line calculator:
calc 10 / 2
5
calc 10 / 0
error: DivisionByZero- Write a program that loads a configuration file and applies these rules:
| Failure | Behavior |
|---|---|
| file missing | use defaults |
| invalid config | print error and stop |
| out of memory | return error immediately |
Write a function that retries a failing operation three times before returning the final error.
Write a small library with public functions that return explicit error sets.
Write a wrapper function that converts low-level filesystem errors into application-level errors.
Review a previous chapter program and identify:
- where
tryshould be used - where
catchshould be used - where
deferbelongs - where
errdeferbelongs - where the program boundary should handle errors
- Write a complete program that:
- reads a file
- parses integers from it
- allocates memory dynamically
- handles malformed input
- frees all resources correctly
- prints useful error messages only at the outer boundary
This chapter introduced four central ideas:
| Feature | Purpose |
|---|---|
| error sets | describe failure |
| error unions | combine values and errors |
try | propagate failure |
catch | handle failure |
defer | guaranteed cleanup |
errdefer | cleanup only on error |
These appear throughout Zig programs. Most library APIs use them directly, and most real programs depend on them for resource management and control flow.