---
title: Debug
description: "Various debugging functions from the Core library of Tact"
---

import { Badge } from '@astrojs/starlight/components';

List of functions commonly used for debugging smart contracts in Tact.

Read more about debugging on the dedicated page: [Debugging](/book/debug).

## require

```tact
fun require(condition: Bool, error: String);
```

Checks the `condition` and throws an error with an [exit code](/book/exit-codes) generated from the `error` message if the `condition` is `false{:tact}`. Otherwise, does nothing.

This function is similar to [`throwUnless(){:tact}`](#throwunless), but instead of using the error code directly, it generates it based on the given error message [`String{:tact}`][p].

The algorithm for generating the exit code works as follows:

* First, the [SHA-256](https://en.wikipedia.org/wiki/SHA-2#Hash_standard) hash of the `error` message [`String{:tact}`][p] is obtained.
* Then, its value is read as a 32-bit [big-endian](https://en.wikipedia.org/wiki/Endianness) number modulo 63000 plus 1000, in that order.
* Finally, it's put into the [`.md` compilation report file](/book/compile#report), which resides with the other compilation artifacts in your project's `outputs/` or `build/` directories.

The generated exit code is guaranteed to be outside the common 0–255 range reserved for TVM and Tact contract errors, which makes it possible to distinguish exit codes from `require(){:tact}` and any other [standard exit codes](/book/exit-codes).

Usage examples:

```tact
// now() has to return a value greater than 1000, otherwise an error message will be thrown
require(now() > 1000, "We're in the first 1000 seconds of 1 January 1970!");

try {
    // The following will never be true, so this require would always throw
    require(now() < -1, "Time is an illusion. Lunchtime doubly so.");
} catch (e) {
    // e will be outside of range 0-255
    dump(e);
}
```

## dump

<Badge text="500+ gas" title="Uses 500 gas units or more" variant="danger" size="medium"/><p/>

```tact
fun dump(arg);
```

Prints the argument `arg` to the contract's debug console. Evaluated only if the `debug` option in the [configuration file](/book/config) is set to `true{:json}`. Otherwise does nothing.

This function is computationally expensive and consumes a lot of gas because it prints the location from which it was called—i.e., the filename, line, and column numbers—and the original expression that was the `arg` argument.

Can be applied to the following list of types and values:

* [`Int{:tact}`][int]
* [`Bool{:tact}`][bool]
* [`Address{:tact}`][p]
* [`Cell{:tact}`][cell], [`Builder{:tact}`][builder], or [`Slice{:tact}`][slice]
* [`String{:tact}`][p]
* [`map<K, V>{:tact}`](/book/maps)
* [Optionals and `null{:tact}` value](/book/optionals)
* `void`, which is implicitly returned when a function doesn't have a return value defined

Usage examples:

```tact
// Int
dump(42); // prints:
          // File filename.tact:2:1
          // dump(42)
          // 42

// Bool
dump(true);
dump(false);

// Address
dump(myAddress());

// Cell, Builder, or Slice
dump(emptyCell());  // Cell
dump(beginCell());  // Builder
dump(emptySlice()); // Slice

// String
dump("Hello, my name is...");

// Maps
let m: map<Int, Int> = emptyMap();
m.set(2 + 2, 4);
dump(m);

// Special values
dump(null);
dump(emit("msg".asComment())); // As emit() function doesn't return a value, dump() would print #DEBUG#: void.
```

:::note[Useful links:]

  [Debug with `dump(){:tact}`](/book/debug#tests-dump)

:::

## dumpStack

<Badge text="500+ gas" title="Uses 500 gas units or more" variant="danger" size="medium"/><p/>

```tact
fun dumpStack();
```

Prints the total stack depth and up to 255 of its values from the top to the contract's debug console. The values are positioned bottom-up—from the deepest value on the left to the topmost value on the right. Evaluated only if the `debug` option in the [configuration file](/book/config) is set to `true{:json}`. Otherwise does nothing.

Usage example:

```tact
dumpStack(); // prints:
             // File filename.tact:1:1
             // dumpStack()
             // stack(3 values) : 100000000 C{96...C7} 0
```

:::note[Useful links:]

  [Debug with `dump(){:tact}`](/book/debug#tests-dump)\
  [Assembly functions](/book/assembly-functions)

:::

## throw

```tact
fun throw(code: Int);
```

Unconditionally throws an exception with an error `code`.

Execution of the current context stops, statements after `throw` are not executed, and control is passed to the first [`try...catch{:tact}` block](/book/statements#try-catch) on the call stack. If there is no `try{:tact}` or `try...catch{:tact}` block among calling functions, [TVM](https://docs.ton.org/learn/tvm-instructions/tvm-overview) terminates the transaction.

Attempts to specify a `code` outside the range $0-65535$ cause an exception with [exit code 5](/book/exit-codes#5): `Integer out of expected range`.

Usage examples:

```tact {2,7}
fun thisWillTerminateAbruptly() {
    throw(1042); // throwing with exit code 1042
}

fun butThisWont() {
    try {
        throw(1042); // throwing with exit code 1042
    }

    // ... follow-up logic ...
}
```

## throwIf

<Badge text="Available since Tact 1.6" variant="tip" size="medium"/><p/>

```tact
fun throwIf(code: Int, condition: Bool);
```

Similar to [`throw(){:tact}`](#throw), but throws an error `code` only if `condition` holds, i.e. is equal to `true{:tact}`. Doesn't throw otherwise.

Attempts to specify a `code` outside the range $0-65535$ cause an exception with [exit code 5](/book/exit-codes#5): `Integer out of expected range`.

Usage example:

```tact {11}
contract Ownership {
    owner: Address;

    init() {
        self.owner = myAddress();
    }

    receive() {
        // Check whether the sender is the owner of the contract,
        // and throw exit code 1024 if not
        throwIf(1024, sender() != self.owner);
    }
}
```

## throwUnless

<Badge text="Available since Tact 1.6" variant="tip" size="medium"/><p/>

```tact
fun throwUnless(code: Int, condition: Bool);
```

Similar to [`throw(){:tact}`](#throw), but throws an error `code` only if `condition` does **not** hold, i.e. if `condition` is not equal to `true{:tact}`. Doesn't throw otherwise.

This function is also similar to [`require(){:tact}`](#require), but uses the specified `code` directly instead of generating one based on a given error message [`String{:tact}`][p].

Attempts to specify a `code` outside the range $0-65535$ cause an exception with [exit code 5](/book/exit-codes#5): `Integer out of expected range`.

Usage example:

```tact {11}
contract Ownership {
    owner: Address;

    init() {
        self.owner = myAddress();
    }

    receive() {
        // Check whether the sender is the owner of the contract,
        // and throw exit code 1024 if not
        throwUnless(1024, sender() == self.owner);
    }
}
```

## nativeThrow

<Badge text="Deprecated since Tact 1.6" variant="tip" size="medium"/><p/>

Use [`throw(){:tact}`](#throw) instead.

```tact
fun nativeThrow(code: Int);
```

An alias to [`throw(){:tact}`](#throw).

## nativeThrowIf

<Badge text="Deprecated since Tact 1.6" variant="tip" size="medium"/><p/>

Use [`throwIf(){:tact}`](#throwif) instead.

```tact
fun nativeThrowIf(code: Int, condition: Bool);
```

An alias to [`throwIf(){:tact}`](#throwif).

## nativeThrowUnless

<Badge text="Deprecated since Tact 1.6" variant="tip" size="medium"/><p/>

Use [`throwUnless(){:tact}`](#throwunless) instead.

```tact
fun nativeThrowUnless(code: Int, condition: Bool);
```

An alias to [`throwUnless(){:tact}`](#throwunless).

[p]: /book/types#primitive-types
[bool]: /book/types#booleans
[int]: /book/integers
[cell]: /book/cells#cells
[builder]: /book/cells#builders
[slice]: /book/cells#slices
