Use a try - except
code block. Catching exceptions is a standard feature of python, like pretty much any modern programming language.
https://docs.python.org/2.7/reference/compound_stmts.html#the-try-statement
Note that if you specify the generic python Exception class in your except clause, you won't catch java-specific exceptions. You would need an extra except clause: