Skip to content

PythonCall.jl how do I handle Python exceptions? #388

Open

Description

@hubert-associates

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

Activity

  1. cjdoris commented on Oct 21, 2023

    @cjdoris
    Member

    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
    
  2. hubert-associates commented on Oct 21, 2023

    @hubert-associates
    Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions