PCAP-31-03 practice question 24 of 25
If you need to serve two different exceptions called Ex1 and Ex2 in one except branch, you can write:
Full set: 147 questionsIn Python you can catch more than one specific exception in a single except-clause by passing a tuple containing the exception classes. The syntax is:
try: ... except (Ex1, Ex2): ...
Because the interpreter expects a tuple after except, only the parenthesised form is accepted.
A. Missing comma or parentheses – invalid syntax.
C. “except Ex1, Ex2:” was removed in Python 3; it is a Python 2 construct that bound Ex2 to the caught exception object.
D. The plus sign is not legal grammar in an except-clause.
• Python 3.11 Language Reference, “8.4 The try statement”, example: except (RuntimeError, TypeError, NameError): … (https://docs.python.org/3/reference/compoundstmts.html#the-try-statement)
• Van Rossum, G., Drake, F. L. “The Python Language Reference Manual”, §5.3 Exceptions (ISBN 0-596-15810-6), p. 81.
Keep practicing
Take the free PCAP-31-03 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other PCAP-31-03 practice questions
- Question 3How many elements will the list2 list contain after execution of the following snippet? l…
- Question 6Select the valid fun () invocations: (select two answers) def fun (a, b=0): return a*b
- Question 7Is it possible to safely check if a class object has a certain attribute0
- Question 10Which of the equations are True? (Select two answers)
- Question 13Which of the following lambda function definitions are correct? (Select two answers)
- Question 15Which of the following invocations are valid? (Select two answers)
