Beginning Programming with Python® For Dummies® by John Paul Mueller

Beginning Programming with Python® For Dummies® by John Paul Mueller

Author:John Paul Mueller [Mueller, John Paul]
Language: eng
Format: epub, pdf
ISBN: 9781119457909
Published: 2018-02-05T00:00:00+00:00


Click Run Cell.

Python displays the expected exception text, as shown in Figure 10-12.

FIGURE 10-12: Raising an exception requires only a call to raise.

Passing error information to the caller

Python provides exceptionally flexible error handling in that you can pass information to the caller (the code that is calling your code) no matter which exception you use. Of course, the caller may not know that the information is available, which leads to a lot of discussion on the topic. If you’re working with someone else’s code and don’t know whether additional information is available, you can always use the technique described in the “Obtaining a list of exception arguments” sidebar earlier in this chapter to find it.

You may have wondered whether you could provide better information when working with a ValueError exception than with an exception provided natively by Python. The following steps show that you can modify the output so that it does include helpful information.

Type the following code into the notebook — pressing Enter after each line: try:

Ex = ValueError()

Ex.strerror = "Value must be within 1 and 10."

raise Ex

except ValueError as e:

print("ValueError Exception!", e.strerror)

The ValueError exception normally doesn’t provide an attribute named strerror (a common name for string error), but you can add it simply by assigning a value to it as shown. When the example raises the exception, the except clause handles it as usual but obtains access to the attributes using e. You can then access the e.strerror member to obtain the added information.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(55895)
What's Done in Darkness by Kayla Perrin(26510)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(18996)
The Fifty Shades Trilogy & Grey by E L James(18947)
Shot Through the Heart by Mercy Celeste(18873)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(16970)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16863)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16793)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16686)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16310)
The Subtle Art of Not Giving a F*ck by Mark Manson(14245)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14063)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13408)
Scorched Earth by Nick Kyme(12704)
Drei Generationen auf dem Jakobsweg by Stein Pia(10911)
Suna by Ziefle Pia(10838)
Scythe by Neal Shusterman(10261)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9468)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9308)
This is Going to Hurt by Adam Kay(9086)