我是 python 的新手,我正在尝试处理错误,但我不知道处理 python 错误的最有效方法。我尝试过这种方式,但理解起来似乎有点复杂。我认为可能存在其他一些“更好”的方式来处理它。
def set_bit(value, pos, nbits):
"""
Set bit at position.
Keyword Arguments
value (int)
Bitstring value.
pos (int)
Position to set bit.
nbits (int)
Number of bits.
"""
if isinstance(value, int):
if value > -1:
# Positives only
if isinstance(pos, int):
if pos > -1:
if isinstance(nbits, int):
if nbits > -1:
return get_bistring(value | 2 ** (pos % nbits), nbits)
else:
raise ValueError(
'"nbits" was set {}, but it must be positive only'
.format(nbits)
)
else:
raise TypeError(
'"nbits" was set {}, but it must be int only'
.format(type(nbits))
)
else:
raise ValueError(
'"pos" was set {}, but it must be positive only'
.format(pos)
)
else:
raise TypeError(
'"pos" was set {}, but it must be int only'
.format(type(pos))
)
else:
raise ValueError(
'"value" was set {}, but it must be positive only'
.format(value)
)
else:
raise TypeError(
'"value" was set {}, but it must be int only'
.format(type(value))
)