Friday , September 20 2024

What is the expected result of executing the following code? try: raise Exception(1, 2, 3) except Exception as e: print(len(e.args))

Questions BankCategory: Python Essentials 2What is the expected result of executing the following code? try: raise Exception(1, 2, 3) except Exception as e: print(len(e.args))
What is the expected result of executing the following code?

try:
    raise Exception(1, 2, 3)
except Exception as e:
     print(len(e.args))
  • The code will print 3
  • The code will print 1
  • The code will print 2
  • The code will raise an unhandled exception

Explanation: The except branch is executed and intercepts the object carrying information about the exception. The args property, which is a tuple, gathers all arguments passed to the Exception class constructor: 1, 2, and 3. The len function computes the length of the tuple (i.e. 3), which is printed to the console.

More Questions: Python Essentials 2 (PE2) Course Final Test