Anonymous user
Flow-control structures: Difference between revisions
→{{header|Python}}: introduced an explicit header structure
m (→goto: Clarified comments) |
(→{{header|Python}}: introduced an explicit header structure) |
||
Line 337:
=={{header|Python}}==
===Exceptions===
<pre>
# Flow Control Structures in Python (Exceptions)
Line 346 ⟶ 347:
def main():
</pre>
<pre>
try:
temp = 0/0
Line 353 ⟶ 356:
print "An error occurred."
# Output : "An error occurred"
</pre>
<pre>
try:
temp = 0/0
Line 361 ⟶ 365:
print "You've divided by zero!"
# Output : "You've divided by zero!"
</pre>
<pre>
try:
temp = 0/0
Line 374 ⟶ 379:
# An error occurred
# End of 'try' block...
</pre>
<pre>
def divisionbyzero(): # create a function that is sure to fail
temp = 0/0
Line 384 ⟶ 390:
# Output :
# You've divided by zero!
</pre>
<pre>
try:
temp = 1/1 # not a division by zero error
Line 395 ⟶ 402:
# Output :
# No apparent error occurred.
</pre>
<pre>
i = 0
while 1: # infinite loop
Line 417 ⟶ 425:
# You've divided by zero. Decrementing i and continuing...
# Imaginary Number! Breaking out of loop
</pre>
<pre>
# Let's call our custom error "StupidError"; it inherits from the Exception class
Line 436 ⟶ 445:
# Output :
# Something stupid occurred: Segfault
</pre>
<pre>
i = 101
for i in range(0,4): # loop 4 times
|