Page 25 - MSDN Magazine, October 2019
P. 25
This will throw a rarely used generic exception type. I’ll talk more about how to create new exception types and so on after I cover how to create classes (and subclasses—anything “raise”d must inherit from a base class).
More to the point, Python allows you to catch these exceptions in much the same way that C#, C++ and Java do, by using the “try” keyword to establish a guarded block of code that, if an exception is raised, will transfer control to one of a series of “except” blocks, with a “finally” clause that will be guaranteed to fire regardless of how control exits the guarded block. What’s new is that Python also allows an “else” clause off of the “try” block, which fires in the event no exception is generated:
# Try/Catch try:
print(“Let’s do bad math”)
impossible = 10 / 0 except ZeroDivisionError:
print(“Can’t divide by zero, man”) else:
print(“That worked?!?”) finally:
print(“Finally clause”)
There are a few forms of exceptions that allow for catching mul- tiple exception types and so on, but the core form is the same. As with C#/CLR exceptions, if the exception passes outside of the top-level function in the currently executing program, the thread will terminate and print a (hopefully helpful) message to the con- sole. Exception types are objects, and custom exception types can be constructed for more precise error-handling logic in more complex programs, but doing so requires understanding how to construct objects in Python, which is still a bit out-of-scope for what I’ve covered so far.
Wrapping Up
For the most part, any experienced object-oriented developer familiar with the language constructs of Java, C#, C++ or any of their kin will find Python’s flow-control constructs to be easily digestible. If anything, Python’s insistence on rigid conformance to the “there’s only one way to do it” mantra makes it that much easier to bring the language into play; there’s no trying to defend when to use “switch/case” instead of a number of “if/else-if ” state- ments, or trying to justify the use of a “do-while” against a “while.” In many respects, this makes Python a much easier language to take on as a first language, which is likely why Python is rapidly becoming the first language for many new programmers, as well as for data scientists who don’t want to program but “ju,st let me do stuff with my data.”
We’re hardly done here, though; Python, like any mainstream programming language, supports the idea of capturing behavior into a named construct—the function—and that’s where we’ll turn our collective heads next. In the meantime ... happy coding! n
Ted Neward is a Seattle-based polytechnology consultant, speaker and mentor. He has written a ton of articles, authored and co-authored a dozen books, and speaks all over the world. Reach him at ted@tedneward.com or read his blog at blogs.tedneward.com.
ThaNks to the following technical expert for reviewing this article: Harry Pierson
msdnmagazine.com
Instantly Search Terabytes
dtSearch’s document filters support: • popular file types
• emails with multilevel attachments • a wide variety of databases
• web data
Over 25 search options including:
• efficient multithreaded search •
• forensics options like credit card search
Developers:
• SDKs for Windows, Linux, macOS
• Cross-platform APIs for C++, Java and .NET with .NET Standard / .NET Core
• FAQs on faceted search, granular data classification, Azure, AWS and more
Visit dtSearch.com for
• hundreds of reviews and case studies
• fully-functional enterprise and developer evaluations
The Smart Choice for Text Retrieval® since 1991
easy
multicolor
hit-
highlighting
dtSearch.com 1-800-IT-FINDS