Question

Which of the following is the correct way to code a try statement that displays the...

Which of the following is the correct way to code a try statement that displays the type and message of the exception that’s caught?

try:

number = int(input("Enter a number: "))

print("Your number is: ", number)

except Exception as e:

print(e(type), e(message))

try:

number = int(input("Enter a number: "))

print("Your number is: ", number)

except Exception as e:

print(type(e), e)

try:

number = int(input("Enter a number: "))

print("Your number is: ", number)

except Exception:

print(Exception(type), Exception(message))

try:

number = int(input("Enter a number: "))

print("Your number is: ", number)

except Exception:

print(type(Exception), Exception)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

try:

number = int(input("Enter a number: "))   
print("Your number is: ", number)

except Exception as e:

print(type(e), e)

Add a comment
Know the answer?
Add Answer to:
Which of the following is the correct way to code a try statement that displays the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Chapter 08 Python Assignment: Question 1-5 Please I need help in my python course. Question 1...

    Chapter 08 Python Assignment: Question 1-5 Please I need help in my python course. Question 1 The finally clause of a try statement a. can be used to recover from an exception b. is required c. can be used to display more information about an exception d. is executed whether or not an exception has been thrown 10 points Question 2 Which of the following is the correct way to code a try statement that catches any type of exception...

  • (Python) Exercise-1: Raising Exceptions Try the following code one by one: raise NameError(‘Name not Found’) raise...

    (Python) Exercise-1: Raising Exceptions Try the following code one by one: raise NameError(‘Name not Found’) raise IndexError("Sorry, index not found!") raise KeyError("Sorry, my fault!") What it does? Exercise-2: Raising Exceptions Try the following code: try : raise NameError( 'Name not found' ) except NameError: print ( 'caught it' ) What it does? Exercise-3: Raising Exceptions using class Here is an example related to RuntimeError. Here, a class is created that is a sub-class from RuntimeError. This is useful when you...

  • 12p I need help this is Python EXCEPTIONS: It's easier to ask forgiveness than permission. Try...

    12p I need help this is Python EXCEPTIONS: It's easier to ask forgiveness than permission. Try the code and catch the errors. The other paradigm is 'Look before you leap' which means test conditions to avoid errors. This can cause race conditions. 1.Write the output of the code here: class MyError(Exception): pass def notZero(num): if num == 0: raise MyError def run(f): try: exec(f) except TypeError: print("Wrong type, Programmer error") except ValueError: print("We value only integers.") except Zero Division Error:...

  • Can you please enter this python program code into an IPO Chart? import random def menu():...

    Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...

  • Python: 1) Consider the following code segment: try : inputFile = open("lyrics.txt", "r") line = inputFile.readline()...

    Python: 1) Consider the following code segment: try : inputFile = open("lyrics.txt", "r") line = inputFile.readline() print(line) _____________________ print("Error") What should be placed in the blank so that the program will print Error instead of crashing if an exception occurs while opening or reading from the file? Group of answer choices a- except RuntimeError : b- except EnvironmentError : c- except IOError : d- except IndexError : 2) Consider the following code segment: line = "hello world!" parts = line.split()...

  • Question 10 (3 points) Which of the following statement is not true? There is a recursive...

    Question 10 (3 points) Which of the following statement is not true? There is a recursive sum method as shown below. When sum (19) is called, summation of all odd numbers less than 19 will be calculated and returned public int sum(int x){ if (x == 0) return 0: else return sum(x-2) + x; The following code segment will throw a testException. This exception has been handled in the way that do nothing but to continue when this exception happens....

  • use python IDEL Please highlight the answer Problem 1 Errors and Exceptions. There are two main...

    use python IDEL Please highlight the answer Problem 1 Errors and Exceptions. There are two main types of errors: syntax errors and runtime errors. Syntax errors are detected by the Python interpreter before the program execution during the parsing stage (parsing is a process, during which the Python interpreter analyzes the grammatical structure of the program). Runtime errors are errors in a program that are not detected by the Python interpreter during its parsing stage. They are further divided into...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • Select the correct answer. (1)     Which of the following will open a file named MyFile.txt and...

    Select the correct answer. (1)     Which of the following will open a file named MyFile.txt and allow you to read data from it?                (a)      File file = new File("MyFile.txt");           (b)      FileWriter inputFile = new FileWriter();           (c)       File file = new File("MyFile.txt");                      FileReader inputFile = new FileReader(file);           (d)      FileWriter inputFile = new FileWriter("MyFile.txt"); (2)     How many times will the following do - while loop be executed?                      int x = 11;             do {             x...

  • Please can someone help me correct this code? def newton(num): temp = 0.000001 estimate = 1.0...

    Please can someone help me correct this code? def newton(num): temp = 0.000001 estimate = 1.0 while True: estimate = (estimate + num / estimate) / 2 difference = abs(num - estimate ** 2) if difference <= temp: break return estimate def main(): while True: try: num = int(input("Enter a positive number or enter/return to quit: ")) print("newton = %0.15f" % newton(num)) except: return main()

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT