Jython Does Not Catch Exceptions
Jython doesn't have the -m option, and it raises an error with from .utils import *. The solution is not to use relative path sys.path.insert(0, os.path.dirname(__file__)) from uti
Solution 1:
Somehow it doesn't like your relative import, I am thinking about 2 possibles solutions:
- Try to import the
utils
package from the root of your project. - Add the path to
utils
package to your PYTHONPATH.
Solution 2:
issubclass(SyntaxError, Exception)
says that it should be caught and indeed, if you raise it manually it is being caught:
try:
raise SyntaxError
except Exception:
print('caught it')
It prints caught it
. Though
try:
from . import *
except Exception:
print('nope')
leads to:
File"<stdin>", line 2SyntaxError: 'import *' not allowed with'from .'
If you add some code above then you see that it is not executed i.e., the latter exception is not runtime. It is the same in CPython e.g.:
import sys
sys.stderr.write("you won't see it\n")
try:
1_ # cause SyntaxError at compile timeexcept:
print("you won't see it either")
The error is raised during compilation.
To workaround the problem, you could try to use absolute imports:
from __future__ import absolute_import
from context.utils import * # both Jython and CPython
Post a Comment for "Jython Does Not Catch Exceptions"