+ 1

Kindly explain why should we use the raise statement.

I clearly understood exception handling with try/except statement. But, not able understand what is meant by the raise statement. Please explain

7th Mar 2017, 10:57 AM
ANAS
ANAS - avatar
1 Resposta
+ 8
Suppose you are writing an adventure text game, and a function to react to a direction order from user: def go_dir(d): if s.lower() in ('north','south','east','west'): /* do the stuff */ else: raise ValueError('Unknown direction') ... later in code you can call it and usually catch the exception if user set a bad value more easily ( and the verification code is factorized at value using place ): try: cmd = input('Command? ') cmd = cmd.split(' ') if cmd[0].lower() == 'go' go_dir(cmd[1]) elif: # do other commands treatments except: # do some error handling
7th Mar 2017, 11:37 AM
visph
visph - avatar