# Parsing one line try catch

**URL:** https://discourse.julialang.org/t/parsing-one-line-try-catch/131756
**Category:** General Usage
**Tags:** parser
**Created:** [August 21, 2025, 5:24pm UTC](https://discourse.julialang.org/t/parsing-one-line-try-catch/131756 "2025-08-21T17:24:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)
#### Post date: [August 21, 2025, 5:24pm UTC](https://discourse.julialang.org/t/parsing-one-line-try-catch/131756/1 "2025-08-21T17:24:00Z")

</div>

I was surprised that the following line does not parse:

```julia-repl
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.11.6 (2025-07-09)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> try 1 catch e1 (-Inf) end
ERROR: ParseError:
# Error @ REPL[1]:1:12
try 1 catch e1 (-Inf) end
# └────────┘ ── a variable name is expected after `catch`
Stacktrace:
 [1] top-level scope
   @ none:1

julia> 

```

Can anyone explain why this fails and what would be the best working alternative? My best working alternative so far is:

```julia
try 1 catch e1 Inf * (-1) end

```

I would prefer a one line solution.

---

<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: [August 21, 2025, 5:50pm UTC](https://discourse.julialang.org/t/parsing-one-line-try-catch/131756/2 "2025-08-21T17:50:59Z")

</div>

> [@GHTaarn](#):
>
> I would prefer a one line solution.

You can just use semicolons:

```julia-auto
julia> try 1; catch e1; (-Inf); end
1

```

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [August 21, 2025, 5:51pm UTC](https://discourse.julialang.org/t/parsing-one-line-try-catch/131756/3 "2025-08-21T17:51:37Z")

</div>

Use semicolons to separate statements if you don’t want literal line breaks

```julia
julia> try 1; catch e1; -Inf; end
1

julia> try error(); catch e1; -Inf; end
-Inf

```

_EDIT: too slow_

---

<div class="post-metadata">

### Author: ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)
#### Post date: [August 21, 2025, 5:57pm UTC](https://discourse.julialang.org/t/parsing-one-line-try-catch/131756/4 "2025-08-21T17:57:10Z")

</div>

I had a vague memory of most control-flow blocks in Julia requiring new-line or `;` to work, but the following works fine (julia v1.11.6):

```julia-auto
if true print("true") else print("false") end

for x in 1:3 println("hello") end

```

Not sure if there is some specific reason that the try-catch block specifically does not behave the same.

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [August 21, 2025, 7:08pm UTC](https://discourse.julialang.org/t/parsing-one-line-try-catch/131756/5 "2025-08-21T19:08:31Z")

</div>

> [@JonasWickman](#):
>
> Not sure if there is some specific reason that the try-catch block specifically does not behave the same.

The issue is when there is parsing ambiguity on where one expression ends and another begins:

```julia-repl
julia> for x in 1:3 x |> println end
1
2
3

julia> for x in 1:3 +x |> println end # parse as `for x in (1:(3+x) |> println) end`
ERROR: UndefVarError: `x` not defined in `Main`

julia> x = 4;

julia> for x in 1:3 +x |> println end # parse as `for x in (1:(3+x) |> println) end`
1:7
ERROR: MethodError: no method matching iterate(::Nothing)

```

---

<div class="post-metadata">

### Author: ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)
#### Post date: [August 22, 2025, 4:03am UTC](https://discourse.julialang.org/t/parsing-one-line-try-catch/131756/6 "2025-08-22T04:03:01Z")

</div>

For it to make sense in the OP, not just ambiguous statements but also “too close for comfort” statements are not allowed, because

```julia-repl
julia> e1 (-Inf)
ERROR: ParseError:
# Error @ REPL[8]:1:3
e1 (-Inf)
# ╙ ── whitespace is not allowed here
Stacktrace:
 [1] top-level scope
   @ none:1

julia> 

```

is not ambiguous.

Whatever the reason, using semicolon is a good solution, it even allows making the statement shorter:

```julia
try 1 catch; -Inf end

```
