# Best way to write nested try/catch blocks

**URL:** https://discourse.julialang.org/t/best-way-to-write-nested-try-catch-blocks/30478
**Category:** New to Julia
**Created:** [October 30, 2019, 4:20am UTC](https://discourse.julialang.org/t/best-way-to-write-nested-try-catch-blocks/30478 "2019-10-30T04:20:31Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [October 30, 2019, 4:20am UTC](https://discourse.julialang.org/t/best-way-to-write-nested-try-catch-blocks/30478/1 "2019-10-30T04:20:31Z")

</div>

I have a piece of code that tries a few things in a nested try/catch block which can look ugly. Is there a better way to write this? Using Macros perhaps? I can imagine wrting a macro with this syntax instead

`@nested\_trycatch zero(T) T(0) rand(T) Vector{T}(undef, 1)[1] throw(“the type $T is not supported”)

```julia
some_elm(::Type{T}) where T = begin
    try
        return zero(T)
    catch
        try
            return T(0)
        catch
            try
                rand(T)
            catch
                try
                    Vector{T}(undef, 1)[1]
                catch
                    throw("the type $T is not supported")
                end
            end
        end
    end
end

```

---

<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: [October 30, 2019, 6:56am UTC](https://discourse.julialang.org/t/best-way-to-write-nested-try-catch-blocks/30478/2 "2019-10-30T06:56:07Z")

</div>

Organize your code differently. Traits would help in general, but may require more investment, `hasmethod` would be the quick & dirty way.

FWIW, if this is related to

> [@What's a good way to get some value (doesn't matter what the value is) of a type \`T\` \`where T\`](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474/):
>
> I am writing some program that requires a value from a type T but it doesn’t matter what this type is. Is there a generic way to get some value of type T for whatever T? Happy to accept T for isbits.

then just use one of the solutions suggested there; if you are OK with random values then it makes little sense to attempt to get a zero first — pretty much the same types have `rand` and `zero` defined.

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [October 31, 2019, 3:23pm UTC](https://discourse.julialang.org/t/best-way-to-write-nested-try-catch-blocks/30478/3 "2019-10-31T15:23:02Z")

</div>

A simple way would be just to flatten it:

```julia
some_elm(::Type{T}) where T = begin
    try
        return zero(T)
    catch end

    try
        return T(0)
    catch end

     try
         return rand(T)
     catch end

     try
         return Vector{T}(undef, 1)[1]
     catch end

     throw("the type $T is not supported")
end

```

Which I would feel is clean enough…especially if you include comments for each attempt that separate the different tries…
