# Try/Catch shortcut

**URL:** https://discourse.julialang.org/t/try-catch-shortcut/48271
**Category:** New to Julia
**Tags:** error
**Created:** [October 13, 2020, 2:03am UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271 "2020-10-13T02:03:15Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)
#### Post date: [October 13, 2020, 2:03am UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/1 "2020-10-13T02:03:15Z")

</div>

Clarification:  
I’m looking for something similar to: `condition ? a : b`  
Except, my condition is when `a` returns some type of error, then do `b` instead of `a`.

```julia
AZ(x)= try; sqrt(x); catch e; return sqrt(complex(x)); end
AZ(2), AZ(-2)

#I want
AZ(a,b)= try; a; catch e; return b; end
AZ(sqrt(-2), sqrt(complex(-2)))

```

---

<div class="post-metadata">

### Author: ![systems](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@systems](https://discourse.julialang.org/u/systems)
#### Post date: [October 13, 2020, 2:19am UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/2 "2020-10-13T02:19:23Z")

</div>

`x = try sqrt(-1); catch e; 0 ; end`

x is now = 0

---

<div class="post-metadata">

### Author: ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)
#### Post date: [October 13, 2020, 2:22am UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/3 "2020-10-13T02:22:58Z")

</div>

I want a function `AZ(a,b)` which first does `a` and if there is an error doing `a`, then does `b`.

---

<div class="post-metadata">

### Author: ![systems](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@systems](https://discourse.julialang.org/u/systems)
#### Post date: [October 13, 2020, 2:26am UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/4 "2020-10-13T02:26:14Z")

</div>

```julia
AZ( a, b) = try sqrt(a) catch; sqrt(b) end 
AZ(-1,4)

```

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [October 13, 2020, 2:35am UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/5 "2020-10-13T02:35:34Z")

</div>

No such function can ever exist in general because functions evaluate all of their arguments, but you want control flow (like the ternary operator provides), so you need to write a macro. Maybe something like the following:

```julia

macro trycatch(a, b)
    quote
        try
            tmp = $(esc(a))
            tmp
        catch
            $(esc(b))
        end
    end
end

let x = 2
    @trycatch(sqrt(x), sqrt(complex(x)))
end

let x = -2
    @trycatch(sqrt(x), sqrt(complex(x)))
end

```

---

<div class="post-metadata">

### Author: ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)
#### Post date: [October 13, 2020, 2:41am UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/6 "2020-10-13T02:41:56Z")

</div>

Thanks! Do you know where `a?b:c` is in the Julia source code?

Currently I’ve been programming an mle solver where loglik returns `-Inf` for illegal parameters:

```julia
    ll(θ;dist=dist,data=data) = try
        -sum(logpdf.(dist{Float64}(θ...),data))
    catch e
        return Inf
    end

```

I’d prefer an operator like `a?$`:  
`ll(θ;dist=dist,data=data)= -sum(logpdf.(dist{Float64}(θ...),data)) @?$ Inf `

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [October 13, 2020, 2:49am UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/7 "2020-10-13T02:49:14Z")

</div>

What do you mean by that? That’s part of Julia’s syntax: it’s in the parser and isn’t a function whose definition you could read. You could write a macro to reuse that syntax for your own needs if you wanted to.

---

<div class="post-metadata">

### Author: ![systems](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@systems](https://discourse.julialang.org/u/systems)
#### Post date: [October 13, 2020, 3:30am UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/8 "2020-10-13T03:30:34Z")

</div>

Also this might be interesting

```julia
tryesce(a, b) =
    for i in b 
        tmp = nothing 
        try
            tmp = a(i)
        catch
        end
        tmp === nothing ? continue : return tmp  
    end

tryesce(sqrt, [-1,-2,4])

```

---

<div class="post-metadata">

### Author: ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)
#### Post date: [October 13, 2020, 4:29pm UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/9 "2020-10-13T16:29:41Z")

</div>

To followup w/ the above example (which works great!):

```julia
macro trycatch(a, b)
    quote
        try
            tmp = $(esc(a))
            tmp
        catch
            $(esc(b))
        end
    end
end
#
f1(x)=@trycatch(sqrt(x), sqrt(complex(x)))
f1(2), f1(-2)

```

is it possible to rewrite: `@trycatch(f(x), g(x))`  
with my own `@$@` as follows: `f(x) @$@ g(x)`  
I wasn’t able to find this in the macro docs.

---

<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: [October 13, 2020, 4:32pm UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/10 "2020-10-13T16:32:47Z")

</div>

No, there’s no such thing as infix macros in Julia, unfortunately.

---

<div class="post-metadata">

### Author: ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)
#### Post date: [October 13, 2020, 4:50pm UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/11 "2020-10-13T16:50:44Z")

</div>

But then how do they allow: `cond ? a : b ` same as `ifelse(cond,a,b)`?

---

<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: [October 13, 2020, 4:52pm UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/12 "2020-10-13T16:52:51Z")

</div>

They are not the same thing:

```julia
julia> true ? println("foo") : println("bar")
foo

julia> ifelse(true, println("foo"), println("bar"))
foo
bar

```

---

<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: [October 13, 2020, 4:54pm UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/13 "2020-10-13T16:54:36Z")

</div>

Also note that the ternary operator is a language feature, not a macro. Therefore, it also can’t be redefined by the user.

---

<div class="post-metadata">

### Author: ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)
#### Post date: [October 13, 2020, 4:56pm UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/14 "2020-10-13T16:56:23Z")

</div>

So bottom line if I have some function:  
`add(a,b)` I cannot rewrite it as `a @$@ b`

---

<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: [October 13, 2020, 4:59pm UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/15 "2020-10-13T16:59:41Z")

</div>

Functions are very different from macros. You can redefine infix operators, because they are just generic functions. Functions can’t transform syntax though. For example, you can define the infix operator `$`:

```julia
julia> a $ b = 10 * a + b
$ (generic function with 1 method)

julia> 1 $ 2
12

```

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [October 13, 2020, 5:00pm UTC](https://discourse.julialang.org/t/try-catch-shortcut/48271/16 "2020-10-13T17:00:31Z")

</div>

I think you’re getting fixated on macro being an infix operator, which isn’t possible but shouldn’t matter that much. You can easily do something like `@t a $ b`.
