# What is the behavior of Error.reject

**URL:** <https://forum.dfinity.org/t/what-is-the-behavior-of-error-reject/5375>\
**Category:** Developers\
**Created:** [June 20, 2021, 1:03pm UTC](https://forum.dfinity.org/t/what-is-the-behavior-of-error-reject/5375 "2021-06-20T13:03:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![flyq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/flyq/32/3637_2.png) [@flyq](https://forum.dfinity.org/u/flyq)\
**Post date:** [June 20, 2021, 1:03pm UTC](https://forum.dfinity.org/t/what-is-the-behavior-of-error-reject/5375/1 "2021-06-20T13:03:57Z")

</div>

Try to use the Error from [base library](https://github.com/dfinity/motoko-base).

```auto
import Error "mo:base/Error";

actor Main {
    var c = 1;

    public func add1() : async Nat {
        c += 100;
        throw Error.reject("test"); 
    };

    public func add2() : async Nat {
        c += 100;
        assert(false);
        return c;
    };

    public func reset() : async Nat {
        c := 1;
        return c;
    };

    public query func getc() : async Nat {
        return c;
    };
};

```

here is my log:

```auto
sudo dfx build main
Password:
Building canisters...

sudo dfx canister --no-wallet install main -m=reinstall
Reinstalling code for canister main, with canister_id rwlgt-iiaaa-aaaaa-aaaaa-cai

dfx canister --no-wallet call main getc
(1)

dfx canister --no-wallet call main add2
The Replica returned an error: code 5, message: "Canister rwlgt-iiaaa-aaaaa-aaaaa-cai trapped explicitly: assertion failed at /Users/flyq/workspace/test/pubfunc/src/main.mo:13.9-13.22"
dfx canister --no-wallet call main getc
(1)

dfx canister --no-wallet call main add1
The Replica returned an error: code 4, message: "test"

dfx canister --no-wallet call main getc
(101)

```

`assert` correctly throw an exception and successfully rolled back the state, meets expectations.

But `Error.reject` throws an exception, but there is no rollback state.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [June 20, 2021, 1:30pm UTC](https://forum.dfinity.org/t/what-is-the-behavior-of-error-reject/5375/2 "2021-06-20T13:30:32Z")

</div>

That is the intended semantics, not a bug.

---

<div class="post-metadata">

**Author:** ![flyq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/flyq/32/3637_2.png) [@flyq](https://forum.dfinity.org/u/flyq)\
**Post date:** [June 20, 2021, 2:37pm UTC](https://forum.dfinity.org/t/what-is-the-behavior-of-error-reject/5375/3 "2021-06-20T14:37:20Z")

</div>

1. If I want to throw an exception in Motoko, I want to be guaranteed the rollback state and throw the related information of the exception, what should I need to do?
2. Is there more information about Error.reject? I find [Overview of canister calling](https://sdk.dfinity.org/docs/interface-spec/index.html#http-call-overview), but not about Error.

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [June 20, 2021, 6:10pm UTC](https://forum.dfinity.org/t/what-is-the-behavior-of-error-reject/5375/4 "2021-06-20T18:10:47Z")

</div>

I found a little bit of info here but I don’t think it fully answers your questions.

> [@Traps and Commit Points - throw confusion](https://forum.dfinity.org/t/traps-and-commit-points-throw-confusion/5253/2):
>
> This is the explicit throw of Motoko, which maps to issuing a “reject” on the system level. On the system level, calls have either a reply or a reject as their response. Don’t confuse throw (aka reject) with the concept of a trap, which happens when you divide by zero. That is not catchable within WebAssembly. Mildly confusing, it often appears that a trap causes a reject, but that is not always the case, and in some sense a trap in the canister causes a the system to response with a reject on …

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [June 20, 2021, 6:50pm UTC](https://forum.dfinity.org/t/what-is-the-behavior-of-error-reject/5375/5 "2021-06-20T18:50:28Z")

</div>

An uncaught/escaping `throw` expression maps to the System API call `msg_reject`, which has the property of committing state changes, just like `msg_reply`. So the high-level semantics is largely determined by that of the low-level platform.

> **[Internet Computer | Documentation](https://sdk.dfinity.org/docs/interface-spec/index.html#system-api-imports)**
>
> Documentation for the internet computer.

Note that throws can be locally caught using `try-catch` expressions, and it would be weird if those caught throws reverted state.
