I'm rather new to python, and I want to make sure I'm doing this correctly. I'd like to have an exception class:
class UnknownCommandReceived(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
I will raise the exception at the end of this function if no regexes match:
def cmdType(self):
match = re.match(r'(<[ \w]+>),\s*(\d+)?,?\s*(\d+)?', cmd, re.IGNORECASE)
if match:
cmd_type = 'int_tool'
return cmd_type, match
match = re.match(r'LCD\(([^\)]*)\)?_?(RED|YELLOW|GREEN|TEAL|BLUE|VIOLET|OFF|ON|SELECT|LEFT|DOWN|RIGHT)?', cmd, re.IGNORECASE)
if match:
cmd_type = 'lcd'
return cmd_type, match
match = re.match(r'buffer(_read|_num|_line)(\((\w)\))?', cmd, re.IGNORECASE)
if match:
cmd_type = 'buffer'
return cmd_type, match
# ... More regex matches ...
raise UnknownCommandReceived( "cmdType received an unknown command" )
# unecessary return?
return 'None', None
My question is--if the exception is always raised, then do I not need a return statement at the end of the function? My apologies.. its a very basic question. My understanding is that an exception once an exception is raised, execution will never return to that point of the code (unless its a loop, or a function that is called again). It will go straight to the catch and continue from there?
No you don't. The return
statement is unreachable.
Also, static-analysis tools, such as pyflakes
will report that as an error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With