# Pycall, Exception Handling of Python errors with Julia's \`try-catch\`

**URL:** <https://discourse.julialang.org/t/pycall-exception-handling-of-python-errors-with-julias-try-catch/50405>\
**Category:** General Usage\
**Tags:** pycall\
**Created:** [November 18, 2020, 11:28pm UTC](https://discourse.julialang.org/t/pycall-exception-handling-of-python-errors-with-julias-try-catch/50405 "2020-11-18T23:28:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![4aHxKzD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/4ahxkzd/32/10833_2.png) [@4aHxKzD](https://discourse.julialang.org/u/4aHxKzD)\
**Post date:** [November 18, 2020, 11:28pm UTC](https://discourse.julialang.org/t/pycall-exception-handling-of-python-errors-with-julias-try-catch/50405/1 "2020-11-18T23:28:16Z")

</div>

Assuming I have the following python code:

```python
import module as m

try:
    # do something
except (m.SomeError, ValueError) as err:
    # do other stuff

```

And that I want to write it in Julia using PyCall, starting with:

```julia
using PyCall

m = pyimport("module")

```

How can I use Julia’s `try-catch` statement to achieve the same result as in the Python code? In other words, I want to catch `m.SomeError` and `ValueError` in Julia.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [November 19, 2020, 8:10pm UTC](https://discourse.julialang.org/t/pycall-exception-handling-of-python-errors-with-julias-try-catch/50405/2 "2020-11-19T20:10:05Z")

</div>

As far as I remember, all Python exceptions are wrapped into a single Julia exception type by PyCall.  
But you can still do your Python specific error handling in Python, e.g.

```julia
py"""
def my_func(x):
    try:
        # do something
    except (m.SomeError, ValueError) as err:
        # do other stuff
    return result"""
my_func = py"my_func" # assign a Julia name
my_func(z)

```

To signal an error state of the Python function, you may pass a sentinel value (e.g. None) to Julia.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 19, 2020, 9:14pm UTC](https://discourse.julialang.org/t/pycall-exception-handling-of-python-errors-with-julias-try-catch/50405/3 "2020-11-19T21:14:08Z")

</div>

> [@4aHxKzD](#):
>
> How can I use Julia’s `try-catch` statement to achieve the same result as in the Python code? In other words, I want to catch `m.SomeError` and `ValueError` in Julia.

PyCall wraps Python errors in a `PyError` type when you catch an exception `err`, but you can extract the underlying Python error class with `err.T` and the error instance with `err.val`.

So, for example, you could do something like:

```julia
m = pyimport("module")
try
    # do something
catch err
    if err isa PyCall.PyError && err.T in (m.SomeError, py"ValueError")
        # do other stuff
    end
end

```

---

<div class="post-metadata">

**Author:** ![4aHxKzD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/4ahxkzd/32/10833_2.png) [@4aHxKzD](https://discourse.julialang.org/u/4aHxKzD)\
**Post date:** [November 20, 2020, 10:55am UTC](https://discourse.julialang.org/t/pycall-exception-handling-of-python-errors-with-julias-try-catch/50405/4 "2020-11-20T10:55:04Z")

</div>

Thanks, that’s exactly what I wanted.
