diff options
author | Levi Bostian <levi.bostian@gmail.com> | 2014-08-03 18:05:46 -0500 |
---|---|---|
committer | Levi Bostian <levi.bostian@gmail.com> | 2014-08-03 18:05:46 -0500 |
commit | a0af5e9a7b0a924cd70e8d3b76f0f9c7eaf0d4d3 (patch) | |
tree | 60299d6f707f1b55405cc6f4f6c374aca1f2df78 | |
parent | 10c07f30d50d9cda4b2ad193b212581c0b32152f (diff) | |
parent | abc06a620bcb41d3e59c5f0c969d819294edc78e (diff) |
Merge pull request #696 from Shashwat986/master
Updated python and python3 with try/except details
-rw-r--r-- | python.html.markdown | 4 | ||||
-rw-r--r-- | python3.html.markdown | 9 |
2 files changed, 10 insertions, 3 deletions
diff --git a/python.html.markdown b/python.html.markdown index aa077e57..5bec5190 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -334,6 +334,10 @@ try: raise IndexError("This is an index error") except IndexError as e: pass # Pass is just a no-op. Usually you would do recovery here. +except (TypeError, NameError): + pass # Multiple exceptions can be handled together, if required. +else: # Optional clause to the try/except block. Must follow all except blocks + print "All good!" # Runs only if the code in try raises no exceptions #################################################### diff --git a/python3.html.markdown b/python3.html.markdown index bc0c05bd..531d3b5a 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -330,9 +330,12 @@ try: raise IndexError("This is an index error") except IndexError as e: pass # Pass is just a no-op. Usually you would do recovery here. +except (TypeError, NameError): + pass # Multiple exceptions can be handled together, if required. +else: # Optional clause to the try/except block. Must follow all except blocks + print("All good!") # Runs only if the code in try raises no exceptions - -# Python's offers a fundamental abstraction called the Iterable. +# Python offers a fundamental abstraction called the Iterable. # An iterable is an object that can be treated as a sequence. # The object returned the range function, is an iterable. @@ -340,7 +343,7 @@ filled_dict = {"one": 1, "two": 2, "three": 3} our_iterable = filled_dict.keys() print(our_iterable) #=> range(1,10). This is an object that implements our Iterable interface -i We can loop over it. +# We can loop over it. for i in our_iterable: print(i) # Prints one, two, three |