March 3, 2020

Python Exception Handling

Exception Handling or Error Handling in Python

"try" ":" suite
("except" [expression ["as" identifier]] ":" suite)+
["else" ":" suite]
["finally" ":" suite]

try:
1/0
except ZeroDivisionError:
print "zerooooo"

try:
1/0
finally:
print "Doing this anyway"

try:
    from Tkinter import Frame, Label, Message, StringVar
except ImportError:
    from tkinter import Frame, Label, Message, StringVar

try:
code
except (NameError, KeyError):
exception code
except:
other exceptions code
else:
else block
finally:
finally block

raise Exception("Row and column indices are required")
raise IndexError("k out of range")
raise SyntaxError(e.message + ':\n' + template)
raise TypeError("unknown mode")
raise ValueError("data has no %d elements: %s"%(number_of_rows, data))

sys.exc_info()
sys.last_type
sys.last_value
sys.last_traceback

Related Artiles:  File Handling in Python   Sets in Python


No comments:

Post a Comment