Flow-control structures: Difference between revisions
Content added Content deleted
(→{{header|Python}}: introduced an explicit header structure) |
(→Case 7 - Creating your own custom exceptions, raise: shorten version) |
||
Line 430: | Line 430: | ||
# Let's call our custom error "StupidError"; it inherits from the Exception class |
# Let's call our custom error "StupidError"; it inherits from the Exception class |
||
class StupidError(Exception): |
class StupidError(Exception): pass |
||
def __init__(self, value): # 'value' is a constructor argument |
|||
self.value = value |
|||
def __str__(self): |
|||
return repr(self.value) |
|||
# Try it out. |
# Try it out. |
||
Line 440: | Line 436: | ||
raise StupidError("Segfault") # here, we manually 'raise' the error within the try block |
raise StupidError("Segfault") # here, we manually 'raise' the error within the try block |
||
except StupidError, details: # 'details' is the StupidError object we create in the try block. |
except StupidError, details: # 'details' is the StupidError object we create in the try block. |
||
print 'Something stupid occurred:', details |
print 'Something stupid occurred:', details # so we access the value we had stored for it... |
||
# Output : |
# Output : |
||
# Something stupid occurred: Segfault |
# Something stupid occurred: Segfault |
||
</pre> |
</pre> |
||
====Case 8 - continue==== |
====Case 8 - continue==== |
||
<pre> |
<pre> |