# throw(ErrorException(...)) vs. @error

**URL:** <https://discourse.julialang.org/t/throw-errorexception-vs-error/46955>\
**Category:** General Usage\
**Created:** [September 20, 2020, 1:40pm UTC](https://discourse.julialang.org/t/throw-errorexception-vs-error/46955 "2020-09-20T13:40:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobcassels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bobcassels/32/14586_2.png) [@bobcassels](https://discourse.julialang.org/u/bobcassels)\
**Post date:** [September 20, 2020, 1:40pm UTC](https://discourse.julialang.org/t/throw-errorexception-vs-error/46955/1 "2020-09-20T13:40:48Z")

</div>

For development / debugging, I’ve been using `throw(ErrorException(...)),` and `@assert`. As this code moves to production, where it will be in a batch job running in “the cloud”, I’d like to have such errors logged.

What’s the recommended Julia way to do this? Should I develop using `@error`, rather than `@assert`, and so forth?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 20, 2020, 1:50pm UTC](https://discourse.julialang.org/t/throw-errorexception-vs-error/46955/2 "2020-09-20T13:50:05Z")

</div>

`@error` is for logging, `throw` is for throwing an exception. They are quite different things so depending on what you want to happen you would chose differently what to use.

---

<div class="post-metadata">

**Author:** ![bobcassels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bobcassels/32/14586_2.png) [@bobcassels](https://discourse.julialang.org/u/bobcassels)\
**Post date:** [September 20, 2020, 2:21pm UTC](https://discourse.julialang.org/t/throw-errorexception-vs-error/46955/3 "2020-09-20T14:21:12Z")

</div>

I understand that they are different. My point was that I want the same code (detecting some problem) to do different things depending on the context. Put me in a debugger with a stack trace if I’m running interactively. Log if the same code is running in “batch mode” on a server.

Is there a smooth Julia way to do that?

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [September 20, 2020, 5:45pm UTC](https://discourse.julialang.org/t/throw-errorexception-vs-error/46955/4 "2020-09-20T17:45:15Z")

</div>

I have a couple of apps in production and the following has served me well.

I use `@error` to log error messages with context (i.e.variables that might help me troubleshoot the problem), immediately followed by `error` if the exception needs to be propagated to the caller.

I use the same same code regardless of the deployment environment.

I don’t use `@assert` in production because I generally want to recover from the error than crashing the program. That might be helpful in tests? I would be delighted to hear about other’s opinions.
