PythonCall.jl
PythonCall.jl copied to clipboard
PythonCall.jl how do I handle Python exceptions?
Using PythonCall.jl, how do I best handle python exception patters such as the following python:
try:
database = client.create_database(DATABASE_NAME)
except exceptions.CosmosResourceExistsError:
database = client.get_database_client(DATABASE_NAME)
This in Julia
julia> try
@pyexec "raise ValueError('foo')"
catch err
if err isa PyException && pyisinstance(err, pybuiltins.ValueError)
@info "handling a ValueError"
else
rethrow()
end
end
[ Info: handling a ValueError
is roughly equivalent to this in Python
>>> try:
... raise ValueError('foo')
... except ValueError as err:
... print('handling a ValueError')
...
handling a ValueError
Sincere thanks for the quick response. Tricky/frustrating to arrive here without this user's guide pattern ....