Jython不捕获异常 [英] Jython does not catch Exceptions

查看:263
本文介绍了Jython不捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jython没有 -m 选项,并从.utils import * 引发错误。



解决方案不是使用相对路径

  sys。 path.insert(0,os.path.dirname(__ file__))
from utils import *

由于我需要使用Jython和CPython的代码,我想出了这个命令:

  try:
从.utils import *
除了异常:#Jython或Python与python context / context.py调用
sys.path.insert(0,os.path.dirname(__ file__))
从utils import *

但是,Jython似乎没有捕获异常,仍然生成异常

 文件context / context.py,第9行
SyntaxError:'import *'不允许与'来自'

根据如何知道解释器是代码中的Jython或CPython?,我试过



<$ p $如果binary为None,则
sys.path.insert(0 *
else:
from .utils import *

我仍然得到 SyntaxError ,为什么Jython解释器在应该这样做的时候继续从.utils import * 解析我的意思是,这段代码可以工作。

  binary = sys.executable 
如果binary是None:
sys .path.insert(0,os.path.dirname(__ file__))
from utils import *
else:
pass

这是我的Jython信息:

  Jython 2.7b1(默认值:在Java1.7.0_51上,
[Java HotSpot(TM)64位服务器虚拟机(Oracle Corporation)]
输入help,copyright 信用或许可证了解更多信息。


解决方案

issubclass(SyntaxError,Exception )说,应该被抓住,确实,如果你手动提高它被抓住:

 尝试:
raise SyntaxError
除了异常:
print('catch it')

它打印抓住它。虽然

  try:
from。进口*
除了例外:
print('nope')

导致:

 文件< stdin>,第2行
SyntaxError:'import *'不允许与' '

如果你在上面添加一些代码,你会发现它不执行,即后一个例外不是运行时。在CPython中是相同的,例如:

  import sys 
sys.stderr.write(你不会看到

try:
1_#因为编译时的语法错误
除了:
print(你不会看到它)

编译过程中出现错误。



要解决问题,您可以尝试使用绝对导入:

 从__future__导入absolute_import 
从context.utils导入*#Jython和CPython


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 utils import *

As I need to use the code both Jython and CPython, I came up with this command:

try:
    from .utils import *
except Exception: # Jython or Python with python context/context.py invocation
    sys.path.insert(0, os.path.dirname(__file__))
    from utils import *

However, Jython doesn't seem to catch the exception, and still generates an exception.

  File "context/context.py", line 9
SyntaxError: 'import *' not allowed with 'from .'

Based on How to know that the interpreter is Jython or CPython in the code?, I tried

binary = sys.executable
if binary is None:
    sys.path.insert(0, os.path.dirname(__file__))
    from utils import *
else:
    from .utils import *

I still get the SyntaxError, why Jython interpreter keeps parsing from .utils import * when it was supposed to do so; I mean, this code works.

binary = sys.executable
if binary is None:
    sys.path.insert(0, os.path.dirname(__file__))
    from utils import *
else:
    pass

This is my Jython information:

Jython 2.7b1 (default:ac42d59644e9, Feb 9 2013, 15:24:52) 
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_51
Type "help", "copyright", "credits" or "license" for more information.

解决方案

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 2
SyntaxError: '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 time
except:
    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

这篇关于Jython不捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆