Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Scoping rules for try - catch - finally

$
0
0

@Eben60 wrote:

I understand that try-catch-finally construct introduces ist own local scope (soft local scope according to https://michaelhatherly.github.io/julia-docs/en/latest/manual/variables-and-scoping.html ). Now I want to do some processing with try/catch/finally AND pass the results to the outer scope. Something like this:

x = 0
y = 0
er = false # or true for another branch

try
    if er
        x = 1
        error()
    else
        x = 2
    end
catch
    x *= 10
finally
    # x *= 100 ### this line would produce an error:
    #### x from try/catch ist not available in finally 
    y = 300
end

println("x=",x, " y=", y)

x =0 y=0

For results from try/catch not available in the finally see discussion in Cumbersome scoping rules for try - catch - finally .

For thist last thing, I could somehow circumwent it by using let block:

let
    x = 0
    y = 0
    er = true # or false for another branch

    try
        if er
            x = 1
            error()
        else
            x = 2
        end
    catch
        x *= 10
    finally
        x *= 100
        y = 300
    end

println("x=",x, " y=", y)
end

println("x=",x, " y=", y)

x=1000 y=300 x=0 y=0

Any idea how to pass the results to the outer scope?

Posts: 9

Participants: 5

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images