Julia: Better example of try/catch in manual

Created on 30 Sep 2017  路  9Comments  路  Source: JuliaLang/julia

The current example in the manual (https://docs.julialang.org/en/latest/manual/control-flow/#The-try/catch-statement-1) and documentation of the keywords (#23861) is as follows:

julia> f(x) = try
           sqrt(x)
       catch
           sqrt(complex(x, 0))
       end

We could really use a better example for this.

Hacktoberfest doc good first issue help wanted

Most helpful comment

I mean more meaningless examples like

f() = try
    error()
catch ex
    return ex
end

So as not to encourage people for using exceptions as control flow.

All 9 comments

Better how? I think any example which alway leads to an error will feel contrived, but an example that only occasionally errors will sometimes fail to demonstrate the point.

The current example is just bad style of programming. The example does not have to always throw IMO.

The problem is that if someone is just learning the language, but for some reason doesn't get to the part of type stability, can pick a wrong idea on how to properly program in Julia. So the point is not that it should be an example that always fails on the try part, but one that follows the general programming practices of the language. I'm pretty sure there are plenty of examples that would fall in this category.

Something like this?

function isopenable(name)
       try
       open(name)
       return true
       catch
       return false
       end
end

Followed by

function isopenable(name)
       try
         open(name)
         return true
       catch ex
         if ex isa SystemError
           return false
         else
           rethrow(ex)
         end
       end
end

The file should be closed.

I don't find the current example problematic but if some people do, I think we should just switch to explicitly throw something so that it's at least clear this is just an example and what error it is catching.
It's not in general the recommanded way of doing control flow anyway so most simple example would not be what you want to actually use in the code...

unreliable_connect() = rand(Bool) ? "connected" : error("failed to connect")
function tryconnect()
    try
        return unreliable_connect()
    catch ex
        if ex isa ErrorException
            return "not connected"
        else
            rethrow(ex)
        end
    end
end

?

I mean more meaningless examples like

f() = try
    error()
catch ex
    return ex
end

So as not to encourage people for using exceptions as control flow.

Hello, I am wondering if could help in some way?

If you'd like to improve the documentation for try/catch, you can update the relevant file (doc/src/manual/control-flow.md in whatever directory you've built Julia) and submit a PR!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tkoolen picture tkoolen  路  3Comments

m-j-w picture m-j-w  路  3Comments

Keno picture Keno  路  3Comments

omus picture omus  路  3Comments

TotalVerb picture TotalVerb  路  3Comments