Exam-style question
Try this first
Explain how exception handling could improve a program that asks a user to enter a number. Include the roles of the try and except blocks in your explanation.
Model answer
What a good answer should say
- The statement that converts the user's input to a number can be placed in a try block because the conversion might fail.
- If the input causes an exception, control can move to the except block.
- The except block can display a suitable message or take another planned action, rather than leaving the program to fail without a response.
- If no exception occurs, the statements following the try and except structure can continue to execute.
Explanation
Why this works
A strong answer identifies the possible exceptional condition, places the potentially failing operation in try, and explains that except provides a response when the exception occurs. It should also make clear that exception handling is a planned response, not a guarantee that all input is valid.
Common mistake
No common mistake is linked to this question yet.
