PythonCall.jl icon indicating copy to clipboard operation
PythonCall.jl copied to clipboard

PythonCall.jl how do I handle Python exceptions?

Open hubert-associates opened this issue 2 years ago • 2 comments

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)

xref stackoverflow

hubert-associates avatar Oct 20 '23 20:10 hubert-associates

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

cjdoris avatar Oct 21 '23 01:10 cjdoris

Sincere thanks for the quick response. Tricky/frustrating to arrive here without this user's guide pattern ....

hubert-associates avatar Oct 21 '23 13:10 hubert-associates