lundi 3 octobre 2016

How do I skip to a certain part of my code if I encounter an error in Julia

I have a 'wrapper function' that takes inputs and just runs a lot of functions in turn and spits out a result at the end. A simple example

function wrapper(a,b)
    c = first_function(a,b)
    d = second_function(c)
    if typeof(d) == Int64
        e = third_function(d)
        f = fourth_function(e)    
        g = fifth_function(f)
        try 
            h = sixth_function(g)
        catch
            i = "this has failed"
        end
        i = seventh_function(h)
    else i = "this has failed"
    end

    return i
end

There are about 5 different places throughout the list of functions that I want to set up 'if - else' statements or 'try - catch' statements. The 'else' part and the 'catch' part are always evaluating the same things. In this example you can see what I mean by seeing that both the else and the catch statements execute i = "this has failed".

Is there a way that I can just define i = "this has failed" at the bottom of the function's code and just tell julia to skip to this line for the 'else' or 'catch' parts ? For example I'd like my above to be something like:

function wrapper(a,b)
    c = first_function(a,b)
    d = second_function(c)
    if typeof(d) == Int64
        e = third_function(d)
        f = fourth_function(e)    
        g = fifth_function(f)
        try 
            h = sixth_function(g)
        catch
            <skip to line 10>
        end
        i = seventh_function(h)
    else <skip to line 10>
    end

    <line 10> i = "this has failed"
    return i
end

Aucun commentaire:

Enregistrer un commentaire