LEARN Python: UP to EXPERT CODING. Are you EXPERT enough in Python programming to ignore THIS? by Dominique SAGE

LEARN Python: UP to EXPERT CODING. Are you EXPERT enough in Python programming to ignore THIS? by Dominique SAGE

Author:Dominique SAGE [SAGE, Dominique]
Language: eng
Format: azw3, epub
Published: 2020-05-19T16:00:00+00:00


Dealing With Errors

In order to better handle program errors and exceptions, it’s best to change what Python displays by default. Keep in mind that at the end of the day it’s the user’s experience that matters, not the programmer’s vision. Achieving perfection is close to impossible, but you can give your program the ability to make your user understand what’s going on when something unexpected happens. That is why in programming we have the so-called “try/except” blocks. The syntax is simple:

try:

Add what your program is supposed to do, except for the following

Exception:

Now, instruct the app on what to do if something mentioned above isn’t possible for whatever reason.

Let’s grab our earlier example and instruct the application to deal with a file that can’t be found:

try:

myFile = open ('employees.csv')

print(myFile.name)

except Exception:

print ("Oops, I can’t find any file named employees.csv in here")

What we do is attempt to open the file, as stated in the “try” section. If the file is opened, then the command within the try statement will be executed and the file will be opened and displayed. However, if the program raises an exception, then it won’t just close with a default error. It will display a message that clearly explains what happens so that any user without programming knowledge can understand.



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(55901)
What's Done in Darkness by Kayla Perrin(26518)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19001)
The Fifty Shades Trilogy & Grey by E L James(18953)
Shot Through the Heart by Mercy Celeste(18877)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(16976)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16869)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16796)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16691)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16316)
The Subtle Art of Not Giving a F*ck by Mark Manson(14251)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14068)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13414)
Scorched Earth by Nick Kyme(12708)
Drei Generationen auf dem Jakobsweg by Stein Pia(10919)
Suna by Ziefle Pia(10843)
Scythe by Neal Shusterman(10265)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9473)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9313)
This is Going to Hurt by Adam Kay(9090)