# New syntax suggestion: catch with type specs

**URL:** <https://discourse.julialang.org/t/new-syntax-suggestion-catch-with-type-specs/101840>\
**Category:** Internals & Design\
**Tags:** exception\
**Created:** [July 20, 2023, 1:51pm UTC](https://discourse.julialang.org/t/new-syntax-suggestion-catch-with-type-specs/101840 "2023-07-20T13:51:14Z")\
**Posts on this page:** 4\
**Page:** 3

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [October 4, 2024, 7:02pm UTC](https://discourse.julialang.org/t/new-syntax-suggestion-catch-with-type-specs/101840/41 "2024-10-04T19:02:02Z")

</div>

> [@anon91151494](#):
>
> To say Julia shouldn’t support something that most other languages support because some Python programmers might bring their bad habits with them really doesn’t make a lot of sense imo.

I don’t think that’s my argument but I’m gonna take care of some other things and maybe if this proposal picks up steam I’ll think about it again at that time.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 4, 2024, 7:23pm UTC](https://discourse.julialang.org/t/new-syntax-suggestion-catch-with-type-specs/101840/42 "2024-10-04T19:23:52Z")

</div>

> [@anon91151494](#):
>
> To say Julia shouldn’t support something that most other languages support

To be clear, Julia supports this just fine (via `if` statements in a `catch` block). It just doesn’t have a specialized syntax.

The question of whether specialized syntax is worth the cost (e.g. of complicating the language) depends on how widely it is used. Julia has been around for \> 10 years — do we see a lot of real-world code that uses `catch` blocks with `if` statements to filter the exceptions, which could be simplified with specialized syntax? Should this pattern be used more frequently?

The fact that `try`–`catch` is not widely used for control flow in Julia (as opposed to e.g. Python), in part for performance reasons, is relevant here because it reduces the demand for a dedicated syntax.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 4, 2024, 7:35pm UTC](https://discourse.julialang.org/t/new-syntax-suggestion-catch-with-type-specs/101840/43 "2024-10-04T19:35:32Z")

</div>

> [@anon91151494](#):
>
> The claim here is that because _some_ authors of Python code use of exceptions wherever possible (something which is already bad design practice) **and** because _specifically in the case of_ _ **Python** _ there is no _performance penalty_ for the use of exceptions, therefore Julia should not support them.

The claim is just that using exceptions for control flow is a bad idea, and that’s worth mentioning because people who come from Python may be used to that style.

It’s _mentioned_ because it’s common in Python. The _judgement_ that it’s a bad idea is independent of Python.

---

<div class="post-metadata">

**Author:** ![Yuan-Ru-Lin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuan-ru-lin/32/46068_2.png) [@Yuan-Ru-Lin](https://discourse.julialang.org/u/Yuan-Ru-Lin)\
**Post date:** [February 21, 2025, 8:58pm UTC](https://discourse.julialang.org/t/new-syntax-suggestion-catch-with-type-specs/101840/44 "2025-02-21T20:58:47Z")

</div>

I am dealing with the same situation and I found a mixture of @mkitti’s and @HanD’s approach clear and useful, that is, dispatching errors to functions whose bodies may span multiple lines.

```julia
try
catch e
    f(e::AssertionError) = println("AssertionError")
    f(e::InexactError) = println("InexactError")
    f(e::DomainError) = begin
        println("Slightly longer implementation here")
   end
   f(e::Exception) = rethrow(e) # rethrow any other kinds of exceptions
   f(e)
end

```

I would like to point out some advantages of the implementation.

1. The appearance of `f`’s doesn’t create cognitive dissonance but people will know they are methods to be dispatched to.
2. The functions remain local in the catch-block, but can be factored out and reused elsewhere anytime you find it appropriate; for example,

```julia
try
catch e
    f(e::AssertionError) = println("error")
    f(e)
end
# f(AssertionError())
# ERROR: UndefVarError: `f` not defined in `Main`

reusable_handle(e::AssertionError) = println("ready to be used in multiple places")
try
catch e
    reusable_handle(e)
end

```

1. No 3rd-party package needed.
2. No new syntax needed

* * *

EDIT: add handling of any other kinds of errors  
EDIT2: fix typo

[Previous page](https://discourse.julialang.org/t/new-syntax-suggestion-catch-with-type-specs/101840.md?page=2)
