# Is panic!() the same as trap()?

**URL:** <https://forum.dfinity.org/t/is-panic-the-same-as-trap/10601>\
**Category:** Developers\
**Created:** [January 31, 2022, 2:27pm UTC](https://forum.dfinity.org/t/is-panic-the-same-as-trap/10601 "2022-01-31T14:27:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)\
**Post date:** [January 31, 2022, 2:27pm UTC](https://forum.dfinity.org/t/is-panic-the-same-as-trap/10601/1 "2022-01-31T14:27:15Z")

</div>

Couldn’t find the answer. There is no such panic hook in `ic-cdk-rs` either.

Will `panic` revert the state? Is it correct to use `Result::expect` and other helpers which `panic`?

---

<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:** [November 4, 2022, 10:05pm UTC](https://forum.dfinity.org/t/is-panic-the-same-as-trap/10601/2 "2022-11-04T22:05:30Z")

</div>

Did you ever get an answer on this?

---

<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:** [November 4, 2022, 10:13pm UTC](https://forum.dfinity.org/t/is-panic-the-same-as-trap/10601/3 "2022-11-04T22:13:33Z")

</div>

Some conflicting info here.

> [@Ic\_cdk::trap equivalent in motoko](https://forum.dfinity.org/t/ic-cdk-trap-equivalent-in-motoko/7101/1):
>
> ic\_cdk::trap can explicitly propagate the error message to the client, and the state changes before trap are reverted.

> [@Can we return Error and trap](https://forum.dfinity.org/t/can-we-return-error-and-trap/13527/2):
>
> You can trap by calling by `ic_cdk::api::trap(&str)`, but the state won’t rollback.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [November 5, 2022, 12:31am UTC](https://forum.dfinity.org/t/is-panic-the-same-as-trap/10601/4 "2022-11-05T00:31:29Z")

</div>

I mean the state before the last async call won’t be rolled back. State changes from the last async till trap is reverted.
