简体   繁体   中英

SystemExit exception not seen as derived from BaseException

I'm currently trying to implement some code, and if it fails, I want to raise an exception with a specific message. I want to use a basic exception, SystemExit, which is supposed to derive from BaseException. I import sys module

I raise the exception this way:

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")

When I hit my case, I end up with an error indicating

TypeError: exceptions must derive from BaseException

I searched for a solution and found this one : I get "TypeError: exceptions must derive from BaseException" even though I did define it and modified my code as:

raise SystemExit("failure retrieving interface's IP address")

but I end up with having the same failure ... Does anyone have any idea of what I'm doing wrong ?

thank you

Alexandre

EDIT: When I go to definition for SystemExit, I get that :

class SystemExit(BaseException):
  """ Request to exit from the interpreter. """

  def __init__(self, args, kwargs):
    pass

  code = None

Well, I don't know what happened but now it works. Actually, due to a new way to extract my ip address, I changed the algorithm in order to get ip scanning data from a file and not from a windows command (arp -a is too limited for my purpose) so I modified the code as follows:

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

I tried both, sys.exti and raise SystemExit and both now work (?). @kevin @ sanket: Thank you for your help and your time

Alexandre

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM