Seite - 106 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 106 -
Text der Seite - 106 -
106 5 SomeMorePythonEssentials
5.2 ExceptionHandling
Anexception, isanerror that isdetectedduringprogramexecution.Weexperienced
suchanerrorpreviouslywithourtimestablesprograminSect.4.2.4.Whenwewere
askedabout3*2,andrepliedwith thewordsix insteadof thenumber6,wecaused
the programto stop and report about someValueError.The programwould have
responded in the same way, for example, if we had rather given 6.0 (i.e., a float) as
input,or justpressedenter (without typinganythingelse).
Ourcodecouldonlyhandle“expected” input fromthe user, i.e., an integerasan
answer to a*b. It would have been much better, however, if it could account also
for “unexpected” input. If possible, we would prefer our program not to stop (or
“crash”) unexpectedly for some kind of input, it should just handle it, get back on
track, and keep on running.Can this be done in Python? Yes! Python has excellent
constructionsfordealingwithexceptionsingeneral,andwewill show,inparticular,
how such exception handling will bring us to the fourth version of our times table
program.
To get the basic idea with exception handling, we will first explain the very
simplesttry-exceptconstruction, and also see how it could be used in the times
tablesprogram.It will onlypartlysolveourproblem,sowe will immediatelymove
on to amore refinedtry-exceptconstruction thatwill be just whatwe need.
Generally,a simpletry-exceptconstructionmaybeputupas
try:
<block of statements> # ...in try block
except:
<block of statements> # ...in except block
# indent reversed, i.e., first line after ‘try-except‘ construction.
Whenexecuted,Pythonrecognizesthe reservedwordstryandexcept (notecolon
and subsequent indent), and will do the following. First, Python will try to execute
the statements in the try block. In the case when these statements execute without
trouble, theexceptblock is skipped(liketheelseblockinanif-elseconstruction).
However,ifsomethinggoeswronginthetryblock,anexception is raisedbyPython,
and execution jumps immediately to the except block without executing remaining
statementsof the tryblock.
It is up to the programmer what statements to have in the except block (as in
the try block, of course), and that makes the programmer free to choose what will
happen when an exception occurs! Sometimes, e.g., a program stop is desirable,
sometimesnot.
5.2.1 TheFourthVersionofOurTimesTablesProgram
Simple Use of try-except Let us now make use of this simple try-except
construction in the main program of times_tables_3.py, as a first attempt to
improvetheprogram.Doingso, thecodemayappearas(wegivethewholeprogram
foreasy reference):
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik