OK, I did the obvious and examined the exception, which is apparently an OSError.  So this code works well:

def xquery(self, query):
try:
result = self.session.query(query).execute()
except OSError as err:
print("Error:", err)
else:
return result

e.g. if I do this call:

q.xquery("7 + 3 = <num>10</num")

I get this output:

Error: Stopped at ., 1/21:
[XPST0003] Expecting '>'.

Posting here in case someone else finds it helpful.

Thanks!

Jonathan

On Tue, Dec 19, 2017 at 11:41 AM, Jonathan Robie <jonathan.robie@gmail.com> wrote:
Thanks!  The try/except part is the part I understand.  The part I don't understand is how to find the exception thrown by BaseX, I want to show syntax errors in a user friendly way like what you see in the GUI so people don't always have to go back to the GUI to figure out what's wrong with their queries.

Jonathan

On Tue, Dec 19, 2017 at 11:02 AM, Christian Grün <christian.gruen@gmail.com> wrote:
> http://jonathanrobie.biblicalhumanities.org/blog/2017/12/08/jupyter-tutorial/
> http://jonathanrobie.biblicalhumanities.org/assets/greeksyntax-tutorial-proiel.html

Looks good!

> But I need to know how to handle exceptions, e.g. if the notebook user
> writes a query with an error in it.

Have you tried to play around with try/except [1]? Well, there are
surely other BaseX users out there that have more experience with
Python.

[1] https://docs.python.org/3/tutorial/errors.html