PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Easy
Question 61
Q164 - Error Handling
The part of your code where you think an exception may occur should be placed inside:
-
A
the
try:branch -
B
the
exception:branch -
C
the
except: branch
Reveal correct answer
Correct answer: A
Explanation
Topics: try except
Try it yourself:
- try:
- # number = input('Please enter a number\n')
- number = 'one'
- zahl = int(number)
- print('Perfekt!')
- except:
- print('Something went wrong.')
- # zahl = int('one') # ValueError: ...
Explanation:
Therefore the name try
Q164 (Please refer to this number, if you want to write me about this question.)
A. The try block is where you place the code that you think may raise an exception. By enclosing this code within a try block, you are indicating to Python that it should attempt to execute this code, and if an exception occurs, it will be caught by the corresponding except block.
B. There is no specific "exception" branch in Python for handling exceptions. The correct structure for handling exceptions is to place the code that may raise an exception inside a try block, and the code that handles the exception inside an except block. This ensures that your program can gracefully handle errors and continue execution.
C. The except block is used to handle exceptions that occur within the try block. When an exception is raised in the try block, Python will look for a matching except block to handle the exception. Placing the code that handles the exception inside the except block ensures that your program can gracefully recover from errors.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
