# About the throw function

**URL:** https://discourse.julialang.org/t/about-the-throw-function/62730
**Category:** Internals & Design
**Created:** [June 11, 2021, 11:19am UTC](https://discourse.julialang.org/t/about-the-throw-function/62730 "2021-06-11T11:19:28Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![wujinq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wujinq/32/22221_2.png) [@wujinq](https://discourse.julialang.org/u/wujinq)
#### Post date: [June 11, 2021, 11:19am UTC](https://discourse.julialang.org/t/about-the-throw-function/62730/1 "2021-06-11T11:19:28Z")

</div>

My question is, why `throw` is a function in Julia? Why is it not a keyword just like the case in many other languages?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [June 11, 2021, 12:08pm UTC](https://discourse.julialang.org/t/about-the-throw-function/62730/2 "2021-06-11T12:08:39Z")

</div>

The short answer it that keywords are only used when necessary.

---

<div class="post-metadata">

### Author: ![wujinq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wujinq/32/22221_2.png) [@wujinq](https://discourse.julialang.org/u/wujinq)
#### Post date: [June 11, 2021, 12:32pm UTC](https://discourse.julialang.org/t/about-the-throw-function/62730/3 "2021-06-11T12:32:36Z")

</div>

That’s the answer I expect (and agree with). Still, the question is now “to what extent can `throw` be regarded as a function”. For example, how can `throw`(which changes the control flow) be implemented using some other functions?

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [June 11, 2021, 1:08pm UTC](https://discourse.julialang.org/t/about-the-throw-function/62730/4 "2021-06-11T13:08:29Z")

</div>

> [@wujinq](#):
>
> For example, how can `throw` (which changes the control flow) be implemented using some other functions?

```julia
mythrow(x) = ccall(:jl_throw, Cvoid, (Any,), x)

```

Note that since it’s not a tfunc like `throw`, it doesn’t have the same type inference support, so in some cases, this might result in worse type inference than `throw`.

---

<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: [June 11, 2021, 1:29pm UTC](https://discourse.julialang.org/t/about-the-throw-function/62730/5 "2021-06-11T13:29:32Z")

</div>

> [@wujinq](#):
>
> the question is now “to what extent can `throw` be regarded as a function”

To the extent that it obeys the semantics of a function, ie you can call it with arguments.

> [@wujinq](#):
>
> can `throw` (which changes the control flow) be implemented using some other functions?

Not without reaching for internals, it is a built-in function.

That said, I am not really sure what the purpose of this discussion is though. Like all languages, Julia needs a primitive to throw exceptions, it has `throw`, and it works fine.

---

<div class="post-metadata">

### Author: ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)
#### Post date: [June 11, 2021, 1:43pm UTC](https://discourse.julialang.org/t/about-the-throw-function/62730/6 "2021-06-11T13:43:01Z")

</div>

One reason is that many static languages have no multiple dispatch, so they can’t have a polymorphic `throw` function, since there are many different types of errors. But Haskell indeed has a `throw` function, and its type signature is quite complicated.  
For some dynamic languages like Python, using a keyword can prevent you redefining the function accidentally. So it’s illegal to write down `raise=1` or `throw=print`…  
Though strictly speaking Julia’s `throw` is a built-in function, not a generic function. But at least it acts like a generic function.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [June 12, 2021, 9:28pm UTC](https://discourse.julialang.org/t/about-the-throw-function/62730/7 "2021-06-12T21:28:11Z")

</div>

> [@Oscar\_Smith](#):
>
> The short answer it that keywords are only used when necessary.

That makes me think `if` could be a small macro instead of a keyword. `for`, too. That would make the language simpler and things like symbolic execution easier.

[https://docs.julialang.org/en/v1/base/base/#Keywords](https://docs.julialang.org/en/v1/base/base/#Keywords)

---

<div class="post-metadata">

### Author: ![jessymilare](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jessymilare/32/13750_2.png) [@jessymilare](https://discourse.julialang.org/u/jessymilare)
#### Post date: [June 13, 2021, 4:48pm UTC](https://discourse.julialang.org/t/about-the-throw-function/62730/8 "2021-06-13T16:48:35Z")

</div>

My 2c. I think it makes much more sense for `throw` to be a built-in function in Julia. There are also other built-in functions which are impossible to implement without accessing internal functions (e.g. `getfield` and `setfield!`). If you need a method, you can wrap these functions in some custom function (e.g. `getproperty` and `setproperty!`).

I understand why other languages implement `throw` as a keyword, but I don’t think it makes sense to do that in Julia. Take Scheme languages, for instance, there are a lot of them which implement [Call/cc](https://en.wikipedia.org/wiki/Call-with-current-continuation), which changes control flow in a much more complex way than Julia’s `throw` (or any language’s `throw` keyword) and they are still functions.
