# Docs: error and exceptions explanation

**URL:** <https://discourse.julialang.org/t/docs-error-and-exceptions-explanation/50624>\
**Category:** New to Julia\
**Tags:** documentation, error, error-message\
**Created:** [November 23, 2020, 12:41pm UTC](https://discourse.julialang.org/t/docs-error-and-exceptions-explanation/50624 "2020-11-23T12:41:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 23, 2020, 12:41pm UTC](https://discourse.julialang.org/t/docs-error-and-exceptions-explanation/50624/1 "2020-11-23T12:41:34Z")

</div>

I was reading the docs carefully, and this section seems confuse:

[https://docs.julialang.org/en/v1/manual/control-flow/#Errors](https://docs.julialang.org/en/v1/manual/control-flow/#Errors)

The section starts with the example of “Suppose we want to stop execution immediately if the square root of a negative number is taken.”, and defines a custom exception for a sqrt function, etc.

However, the standard `sqrt` also stops the execution immediately if the argument is negative (or it does not?).

Thus, it is not clear to me if the example is only showing the custom definition of an error message, or if there is something else under the hood that should be explained.

Similarly, it is not clear in the previous section, [Throw function](https://docs.julialang.org/en/v1/manual/control-flow/#The-%5Bthrow%5D(@ref)-function), which is the utility of defining custom exception errors, or even what exception types provide besides a different error message each. I guess that we can catch the type of error in a `try` block and deal with it later. In that case probably the `Try` section should come before that?

In the way it is organized and written, one has impression that standard exception errors will not stop the program immediately, and that one should read the output as something like `r = sqrt(-1) ; if r isa(Error)` etc, and if we wanted that they stop the program, a new custom error must be defined for the function in question.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 23, 2020, 1:48pm UTC](https://discourse.julialang.org/t/docs-error-and-exceptions-explanation/50624/2 "2020-11-23T13:48:40Z")

</div>

> [@lmiq](#):
>
> However, the standard `sqrt` also stops the execution immediately if the argument is negative (or it does not?).

Yes, it does, the documentation says so, and the REPL agrees with it:

```julia
  sqrt(x)

  Return \sqrt{x}. Throws DomainError for negative Real arguments. Use complex
  negative arguments instead. The prefix operator √ is equivalent to sqrt.

julia> sqrt(-1)
ERROR: DomainError with -1.0:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).

```

It seems to me that, maybe, this section was written a long time ago, when `sqrt` did not throw? It could be updated to a less confusing example. But is even more strange because in the same page, a little above, inside section [Built-in Exceptions](https://docs.julialang.org/en/v1/manual/control-flow/#Built-in-Exceptions) the example of built-in exception is exactly it: “For example, the [`sqrt`](https://docs.julialang.org/en/v1/base/math/#Base.sqrt-Tuple%7BReal%7D) function throws a [`DomainError`](https://docs.julialang.org/en/v1/base/base/#Core.DomainError) if applied to a negative real value:”

> [@lmiq](#):
>
> which is the utility of defining custom exception errors, or even what exception types provide besides a different error message each. I guess that we can catch the type of error in a `try` block and deal with it later. In that case probably the `Try` section should come before that?

Yes, I think the main utilities are (i) being able to chose if capture or not based on the type of the exception and (ii) that you may write a constructor that just take some data and will assemble the string for you, on demand, if it needs to be shown.

It is possible that, for the beginner in a language with exceptions, it would be better to have the `Try` section before the `throw` section. It was probably an oversight in which the writers assumed the reader already had some familiarity with the topic.

What bugged me most in the `throw` section is the “Additionally, some exception types take one or more arguments that are used for error reporting:” that comes after all section already was using exception types with one or more arguments, it seems to me that someone re-wrote the examples above and forgot to remove or move this paragraph below.

> [@lmiq](#):
>
> In the way it is organized and written, one has impression that standard exception errors will not stop the program immediately, and that one should read the output as something like `r = sqrt(-1) ; if r isa(Error)` etc, and if we wanted that they stop the program, a new custom error must be defined for the function in question.

I am not sure if I follow you train of thought. Just creating an exception is just creating an object of that type and no different than creating an instance of any other `struct`, what stops the execution and flows it to the innermost `try`-`catch` that encloses the code is calling `throw`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 23, 2020, 1:53pm UTC](https://discourse.julialang.org/t/docs-error-and-exceptions-explanation/50624/3 "2020-11-23T13:53:46Z")

</div>

> [@Henrique\_Becker](#):
>
> I am not sure if I follow you train of thought.

I just wanted to mean that, as written there, one might think that `sqrt(-1)` would return an exception message but that would not stop the execution, in such a way that we would have to decide what to do with that, as if every error in any internal function was implemented in a `try` block.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 23, 2020, 2:42pm UTC](https://discourse.julialang.org/t/docs-error-and-exceptions-explanation/50624/4 "2020-11-23T14:42:12Z")

</div>

> [@Henrique\_Becker](#):
>
> maybe, this section was written a long time ago, when `sqrt` did not throw?

Cf

> <https://github.com/JuliaLang/julia/pull/22202>
>
> While thinking about #18521/#20005 a bit more, I realized that perhaps an easier… approach would be to ensure that every creation of certain exception types occurred from Julia code. To cover both \`InexactError\` and \`DomainError\` (probably our two most irritating errors), that required Julia replacements for 4 intrinsics.
> 
> I've checked that the LLVM code generation is identical, but it's probably a good idea to @nanosoldier \`runbenchmarks((ALL, vs = ":master")\`.

Perhaps the docs could be improved by calling it `mysqrt`, and just suggesting that this is how it can implemented (removing all references to “fussy”).
