PCAP Exam QuestionsBrowse all questions from this exam

PCAP Exam - Question 67


If you need to serve two different exceptions called Ex1 and Ex2 in one except branch, you can write:

Show Answer
Correct Answer: B

If you need to handle multiple exceptions in a single except branch in Python, you should use parentheses to group them. Writing 'except (Ex1, Ex2):' correctly instructs Python to handle both Ex1 and Ex2 exceptions. This syntax allows you to specify a tuple of exception classes that the except clause will catch, making option B the correct choice.

Discussion

11 comments
Sign in to comment
kauserOption: B
Aug 25, 2020

Answer is C, but with brackets inserted around (Ex1, Ex2) - or B uppercase Ex1

Fulano_de_talOption: D
Mar 17, 2021

The Answer is D: try: pass except TypeError+ZeroDivisionError: pass except: pass

leonardodicaprio
Jun 25, 2021

ur code is giving issue. It can't be an answer

macxszOption: B
May 3, 2022

B. except (Ex1, Ex2):

BenKaOption: B
Aug 11, 2020

B should be the answer, but aren't the Exception name case sensitive? In which case, B is not the answer.

rbishunOption: B
Nov 4, 2021

# Answer is B. Point of Q is, you need to use () with except when more than 1. try: # TypeError: can only concatenate str (not "int") to str print( 'five' + 1 ) # ZeroDivisionError: division by zero print ( 5 / 0) except (TypeError, ZeroDivisionError): print('An error occured.')

BrodehOption: A
Mar 20, 2021

Answer should be A

tanstOption: D
Oct 18, 2021

Should be D

DKMOption: B
Nov 4, 2021

If you need to serve two different exceptions called Ex1 and Ex2 in one except branch, you can write: a. except Ex1 Ex2: b. except (Ex1, Ex2): c. except Ex1, Ex2: d. except Ex1 + Ex2:

technoguyOption: B
Nov 27, 2021

B is corrrect answer

rocky48Option: B
Apr 22, 2022

b. except (Ex1, Ex2):

seaverickOption: B
Jan 29, 2024

try: # TypeError: can only concatenate str (not "int") to str print( 'five' + 1 ) # ZeroDivisionError: division by zero print ( 5 / 0) except (TypeError, ZeroDivisionError): #correct #except TypeError, ZeroDivisionError: #SyntaxError: multiple exception types must be parenthesized #except TypeError ZeroDivisionError: #SyntaxError: invalid syntax #except TypeError + ZeroDivisionError: #TypeError: unsupported operand type(s) for +: 'type' and 'type' print('An error occured.') Output - An error ocurred Ans is B