Hone logo
Hone
Problems

Robust Error Handling with Rust Panics

Rust's panic system provides a mechanism for handling unrecoverable errors. This challenge focuses on implementing robust panic handling using panic! and catch_unwind, ensuring your program gracefully recovers or terminates when unexpected situations arise. Understanding panic handling is crucial for writing reliable and stable Rust applications.

Problem Description

You are tasked with creating a program that demonstrates different aspects of panic handling in Rust. The program should include functions that might panic under certain conditions and implement mechanisms to catch and potentially recover from these panics. Specifically, you need to:

  1. Create a function divide(x: i32, y: i32): This function should divide x by y. If y is zero, it should panic! with a descriptive message.
  2. Create a function potentially_panicking_function(): This function should call divide(10, 0).
  3. Implement a main function: The main function should call potentially_panicking_function() within a catch_unwind block.
  4. Handle the panic: Inside the catch_unwind block, check if a panic occurred. If a panic occurred, print the panic message to the console. If no panic occurred, print a success message.
  5. Demonstrate unwinding: The program should unwind the stack when a panic occurs, allowing for cleanup actions if necessary (though this challenge doesn't explicitly require cleanup).
  6. Demonstrate non-unwinding: Create a separate function non_unwinding_function() that panics. Call this function within a panic::catch_unwind block configured to not unwind. Verify that the program terminates immediately upon the panic, without attempting to unwind the stack.

Examples

Example 1:

Input: Calling potentially_panicking_function()
Output: Panic: division by zero
Explanation: The divide function panics when y is zero. The catch_unwind block catches the panic and prints the panic message.

Example 2:

Input: Calling a function that doesn't panic
Output: Success! No panic occurred.
Explanation: The divide function does not panic, so the catch_unwind block executes normally and prints the success message.

Example 3: (Non-unwinding example)

Input: Calling non_unwinding_function()
Output: Program terminates immediately with a panic message.
Explanation: The panic::catch_unwind block is configured to not unwind. The panic occurs, and the program terminates without attempting stack unwinding.

Constraints

  • The program must compile and run without errors.
  • The panic message for division by zero must be "division by zero".
  • The catch_unwind block must correctly catch and handle panics.
  • The non-unwinding example must terminate immediately upon panic.
  • The code should be well-formatted and readable.
  • The program should be self-contained and not rely on external dependencies beyond the Rust standard library.

Notes

  • catch_unwind is a powerful tool for handling panics, but it should be used judiciously. Consider alternatives like Result for recoverable errors.
  • The panic! macro provides a simple way to trigger a panic.
  • The panic::catch_unwind function allows you to specify whether the stack should be unwound when a panic occurs. Unwinding can be expensive, so it's important to consider the performance implications.
  • Think about the difference between unwinding and non-unwinding panics and how they affect program behavior. Non-unwinding panics are generally faster but can lead to abrupt termination.
Loading editor...
rust