4314.3640684.qx3zqy7 In the code below, we compute the sum of integers in a file by reading them one line at a time. Rearrange the following lines of code so that the input file is closed even if the int function raises an exception. Follow Programming Tip "Do not use except and finally in the same try statement" in the Exception Handling section by placing the try block and finally clause inside the outer try block. How to use this tool v

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter14: Exception Handling
Section: Chapter Questions
Problem 3SA
icon
Related questions
Question
CHALLENGE 7.26.1: Rearrange the code so that the input file is closed even if the int function raises an exception.
ACTIVITY
494314.3640684.qx3zqy7
In the code below, we compute the sum of integers in a file by reading them one line at a time.
Rearrange the following lines of code so that the input file is closed even if the int function raises an exception. Follow
Programming Tip "Do not use except and finally in the same try statement" in the Exception Handling section by placing the try
block and finally clause inside the outer try block.
How to use this tool V
Unused
total = total + value
try :
finally :
infile = open (input())
for line in infile :
total = 0
print("Total:", total)
print("Closing file")
infile.close()
value= int(line)
Tester.py
try :
except ValueError:
Load default template...
print("Error: Not an integer value.")
Transcribed Image Text:CHALLENGE 7.26.1: Rearrange the code so that the input file is closed even if the int function raises an exception. ACTIVITY 494314.3640684.qx3zqy7 In the code below, we compute the sum of integers in a file by reading them one line at a time. Rearrange the following lines of code so that the input file is closed even if the int function raises an exception. Follow Programming Tip "Do not use except and finally in the same try statement" in the Exception Handling section by placing the try block and finally clause inside the outer try block. How to use this tool V Unused total = total + value try : finally : infile = open (input()) for line in infile : total = 0 print("Total:", total) print("Closing file") infile.close() value= int(line) Tester.py try : except ValueError: Load default template... print("Error: Not an integer value.")
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 3 images

Blurred answer
Knowledge Booster
Exception Handling Keywords
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning