Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 106 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 106 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 106 -

Image of the Page - 106 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 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):
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python 3.6
Volume
Second Edition
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2020
Language
English
License
CC BY 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
356
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python