SystemExit 异常不被视为派生自 BaseException
SystemExit exception not seen as derived from BaseException
我目前正在尝试实现一些代码,如果它失败了,我想用特定消息引发异常。
我想使用一个基本异常 SystemExit,它应该派生自 BaseException。我导入系统模块
我这样抛出异常:
add_ip = None
# search for the ip address associated with the equipment's mac address
for i in range(0, len(add_match)):
found = add_match[i].find(add_mac)
if found != -1:
add_ip = add_match[i].split()[0]
// it turns out that I don't find the matching IP address
if add_ip is not None:
print("@IP =\t", add_ip) # log matching IP
else:
raise (SystemExit, "failure retrieving interface's IP address")
当我遇到我的情况时,我最终得到一个错误指示
TypeError: exceptions must derive from BaseException
我搜索了一个解决方案并找到了这个:I get "TypeError: exceptions must derive from BaseException" even though I did define it
并将我的代码修改为:
raise SystemExit("failure retrieving interface's IP address")
但我最终遇到了同样的失败......
有谁知道我做错了什么吗?
谢谢
亚历山大
编辑:
当我转到 SystemExit 的定义时,我得到了:
class SystemExit(BaseException):
""" Request to exit from the interpreter. """
def __init__(self, args, kwargs):
pass
code = None
嗯,
我不知道发生了什么,但现在可以了。
实际上,由于一种提取 ip 地址的新方法,我更改了算法以便从文件而不是 windows 命令获取 ip 扫描数据(arp -a 对我的目的来说太有限了)所以我修改代码如下:
add_ip = None
# parse a previousely saved Angry-IP export
try:
with open ("current_ip_scan.txt", "r") as scan_file:
for line in scan_file:
line = line.upper()
line = re.sub(r':','-', line)
if (re.search(add_mac, line)) is not None:
add_ip = re.findall('(([0-9]{2,3}\.){3}[0-9]{3})',line)[0][0] # to get the first element of the tuple
except FileNotFoundError:
print("*** Angry-IP export not available - do it into 'current_ip_scan.txt' in order to find the matching IP address ***")
raise SystemExit("failure retrieving interface's IP address")
if add_ip is not None:
# check that the ip address is a valid IP
if(re.match('(([0-9]{2,3}\.){3}[0-9]{3})', add_ip)) is not None:
print("@IP =\t", add_ip) # log matching IP
else:
raise SystemExit("failure retrieving interface's IP address")
else:
#sys.exit("failure retrieving interface's IP address")
raise SystemExit("failure retrieving interface's IP address")
return add_ip
我都试过了,sys.exti 和 raise SystemExit 现在都可以工作了(?)。
@kevin @sanket:感谢你的帮助和你的时间
亚历山大
我目前正在尝试实现一些代码,如果它失败了,我想用特定消息引发异常。 我想使用一个基本异常 SystemExit,它应该派生自 BaseException。我导入系统模块
我这样抛出异常:
add_ip = None
# search for the ip address associated with the equipment's mac address
for i in range(0, len(add_match)):
found = add_match[i].find(add_mac)
if found != -1:
add_ip = add_match[i].split()[0]
// it turns out that I don't find the matching IP address
if add_ip is not None:
print("@IP =\t", add_ip) # log matching IP
else:
raise (SystemExit, "failure retrieving interface's IP address")
当我遇到我的情况时,我最终得到一个错误指示
TypeError: exceptions must derive from BaseException
我搜索了一个解决方案并找到了这个:I get "TypeError: exceptions must derive from BaseException" even though I did define it 并将我的代码修改为:
raise SystemExit("failure retrieving interface's IP address")
但我最终遇到了同样的失败...... 有谁知道我做错了什么吗?
谢谢
亚历山大
编辑: 当我转到 SystemExit 的定义时,我得到了:
class SystemExit(BaseException):
""" Request to exit from the interpreter. """
def __init__(self, args, kwargs):
pass
code = None
嗯, 我不知道发生了什么,但现在可以了。 实际上,由于一种提取 ip 地址的新方法,我更改了算法以便从文件而不是 windows 命令获取 ip 扫描数据(arp -a 对我的目的来说太有限了)所以我修改代码如下:
add_ip = None
# parse a previousely saved Angry-IP export
try:
with open ("current_ip_scan.txt", "r") as scan_file:
for line in scan_file:
line = line.upper()
line = re.sub(r':','-', line)
if (re.search(add_mac, line)) is not None:
add_ip = re.findall('(([0-9]{2,3}\.){3}[0-9]{3})',line)[0][0] # to get the first element of the tuple
except FileNotFoundError:
print("*** Angry-IP export not available - do it into 'current_ip_scan.txt' in order to find the matching IP address ***")
raise SystemExit("failure retrieving interface's IP address")
if add_ip is not None:
# check that the ip address is a valid IP
if(re.match('(([0-9]{2,3}\.){3}[0-9]{3})', add_ip)) is not None:
print("@IP =\t", add_ip) # log matching IP
else:
raise SystemExit("failure retrieving interface's IP address")
else:
#sys.exit("failure retrieving interface's IP address")
raise SystemExit("failure retrieving interface's IP address")
return add_ip
我都试过了,sys.exti 和 raise SystemExit 现在都可以工作了(?)。 @kevin @sanket:感谢你的帮助和你的时间
亚历山大