misc/process/wait.html)
    9. [ **20.6.** Filesystem Operations ](../std_misc/fs.html)
    10. [ **20.7.** Program arguments ](../std_misc/arg.html) â± 
    11.       1. [ **20.7.1.** Argument parsing ](../std_misc/arg/matching.html)
    12. [ **20.8.** Foreign Function Interface ](../std_misc/ffi.html)
  41. [ **21.** Testing ](../testing.html) â± 
  42.     1. [ **21.1.** Unit testing ](../testing/unit_testing.html)
    2. [ **21.2.** Documentation testing ](../testing/doc_testing.html)
    3. [ **21.3.** Integration testing ](../testing/integration_testing.html)
    4. [ **21.4.** Dev-dependencies ](../testing/dev_dependencies.html)
  43. [ **22.** Unsafe Operations ](../unsafe.html) â± 
  44.     1. [ **22.1.** Inline assembly ](../unsafe/asm.html)
  45. [ **23.** Compatibility ](../compatibility.html) â± 
  46.     1. [ **23.1.** Raw identifiers ](../compatibility/raw_identifiers.html)
  47. [ **24.** Meta ](../meta.html) â± 
  48.     1. [ **24.1.** Documentation ](../meta/doc.html)
    2. [ **24.2.** Playground ](../meta/playground.html)

__ __

  * Light (default) 
  * Rust 
  * Coal 
  * Navy 
  * Ayu 

__

#  Rust By Example

[ __ ](../print.html "Print this book") [ __ ](https://github.com/rust-
lang/rust-by-example "Git repository") [ __ ](https://github.com/rust-
lang/rust-by-example/edit/master/src/std/panic.md "Suggest an edit")

#  ` panic! `

The ` panic! ` macro can be used to generate a panic and start unwinding its
stack. While unwinding, the runtime will take care of freeing all the
resources _owned_ by the thread by calling the destructor of all its objects.

Since we are dealing with programs with only one thread, ` panic! ` will cause
the program to report the panic message and exit.

    
    
    // Re-implementation of integer division (/)
    fn division(dividend: i32, divisor: i32) -> i32 {
        if divisor == 0 {
            // Division by zero triggers a panic
            panic!("division by zero");
        } else {
            dividend / divisor
        }
    }
    
    // The `main` task
    fn main() {
        // Heap allocated integer
        let _x = Box::new(0i32);
    
        // This operation will trigger a task failure
        division(3, 0);
    
        println!("This point won't be reached!");
    
        // `_x` should get destroyed at this point
    }
    

Let's check that ` panic! ` doesn't leak memory.

    
    
    $ rustc panic.rs && valgrind ./panic
    ==4401== Memcheck, a memory error detector
    ==4401== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
    ==4401== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
    ==4401== Command: ./panic
    ==4401== 
    thread '<main>' panicked at 'division by zero', panic.rs:5
    ==4401== 
    ==4401== HEAP SUMMARY:
    ==4401==     in use at exit: 0 bytes in 0 blocks
    ==4401==   total heap usage: 18 allocs, 18 frees, 1,648 bytes allocated
    ==4401== 
    ==4401== All heap blocks were freed -- no leaks are possible
    ==4401== 
    ==4401== For counts of detected and suppressed errors, rerun with: -v
    ==4401== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
    

[ __ ](../std/result/question_mark.html "Previous chapter") [ __
](../std/hash.html "Next chapter")

[ __ ](../std/result/question_mark.html "Previous chapter") [ __
](../std/hash.html "Next chapter")