Not attempting to compare the languages but just for knowledge,
不是为了比较语言而是为了知识,
Is there any way to have equivalent of java throws
keyword/functionality in Python?
有什么方法可以在Python中拥有与java throw相同的关键字/功能吗?
or the way we can recognize checked exception thrown by any method at static time?
或者我们识别任何方法在静态时抛出的检查异常的方法?
or Passing(chaining) exception handling responsibility?
或者传递(链接)异常处理责任?
Java:
Java:
public void someMethod() throws SomeException
{
}
Python:
Python:
@someDecorator # any way to do?
def someMethod():
pass
2 个解决方案
#1
11
If you can't have statically typed arguments, you can't have static throws declarations. For instance, there's no way for me to annotate this function:
如果不能有静态类型的参数,则不能有静态抛出声明。例如,我没有办法注释这个函数:
def throw_me(x):
raise x
Or even this one:
甚至这个:
def call_func(f):
f() # f could throw any exception
What you can do is make it an error to throw any type of exception other than those specified:
您可以做的是使抛出任何类型的异常都是错误的,除了那些指定的:
from functools import wraps
class InvalidRaiseException(Exception):
pass
def only_throws(E):
def decorator(f):
@wraps(f)
def wrapped(*args, **kwargs):
try:
return f(*args, **kwargs)
except E:
raise
except InvalidRaiseException:
raise
except Exception as e:
raise InvalidRaiseException("got %s, expected %s, from %s" % (
e.__class__.__name__, E.__name__, f.__name__)
)
return wrapped
return decorator
@only_throws(ValueError)
def func(x):
if x == 1:
raise ValueError
elif x == 2:
raise Exception
>>> func(0)
>>> func(1)
ValueError
>>> func(2)
InvalidRaiseException: got Exception, expected ValueError, from func
#2
8
There is no standard equivalent of this in Python as far as I know, and it's not necessary either. The best you can do is indicate in the docstring what exceptions/errors are raised in what circumstances, and leave it to whoever is using your functions to work out the rest.
就我所知,在Python中没有与之对应的标准,而且也没有必要。您所能做的最好的事情是在docstring中指出在什么情况下出现了哪些异常/错误,并将其留给使用您的函数来解决其余问题的人。
In Java, the throws clause is a sort of bookkeeping. For example,
在Java中,throw子句是一种簿记。例如,
try {
foo();
} catch (IOException ioe) {
}
doesn't compile unless foo
is known to have the potential of throwing an IOException
. The analog in Python:
不编译,除非已知foo具有抛出IOException的可能性。Python的模拟:
try:
foo()
except IOError as ioe:
pass
compiles regardless. There is no concept of "checked vs unchecked".
无论如何编译。没有“检查vs未检查”的概念。
#1
11
If you can't have statically typed arguments, you can't have static throws declarations. For instance, there's no way for me to annotate this function:
如果不能有静态类型的参数,则不能有静态抛出声明。例如,我没有办法注释这个函数:
def throw_me(x):
raise x
Or even this one:
甚至这个:
def call_func(f):
f() # f could throw any exception
What you can do is make it an error to throw any type of exception other than those specified:
您可以做的是使抛出任何类型的异常都是错误的,除了那些指定的:
from functools import wraps
class InvalidRaiseException(Exception):
pass
def only_throws(E):
def decorator(f):
@wraps(f)
def wrapped(*args, **kwargs):
try:
return f(*args, **kwargs)
except E:
raise
except InvalidRaiseException:
raise
except Exception as e:
raise InvalidRaiseException("got %s, expected %s, from %s" % (
e.__class__.__name__, E.__name__, f.__name__)
)
return wrapped
return decorator
@only_throws(ValueError)
def func(x):
if x == 1:
raise ValueError
elif x == 2:
raise Exception
>>> func(0)
>>> func(1)
ValueError
>>> func(2)
InvalidRaiseException: got Exception, expected ValueError, from func
#2
8
There is no standard equivalent of this in Python as far as I know, and it's not necessary either. The best you can do is indicate in the docstring what exceptions/errors are raised in what circumstances, and leave it to whoever is using your functions to work out the rest.
就我所知,在Python中没有与之对应的标准,而且也没有必要。您所能做的最好的事情是在docstring中指出在什么情况下出现了哪些异常/错误,并将其留给使用您的函数来解决其余问题的人。
In Java, the throws clause is a sort of bookkeeping. For example,
在Java中,throw子句是一种簿记。例如,
try {
foo();
} catch (IOException ioe) {
}
doesn't compile unless foo
is known to have the potential of throwing an IOException
. The analog in Python:
不编译,除非已知foo具有抛出IOException的可能性。Python的模拟:
try:
foo()
except IOError as ioe:
pass
compiles regardless. There is no concept of "checked vs unchecked".
无论如何编译。没有“检查vs未检查”的概念。