Question

(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 need to display more specific information when an

exception is caught. In the try block, the user-defined exception is raised and caught in the except

block.

Try the following code:

class MyException(Exception):

pass

# MyException classinherits from the Exception class

raise MyException("Caught MyException")

What it does?

Now, add the try/finally block as shown below:

class MyException(Exception):

pass

# MyException classinherits from the Exception class

try:

raise MyException("Caught MyException")

finally:

print ("Done")

What it does?

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

Exercise 1

File Edit View Insert Cell Kernel Widgets Help Trusted Python 3 O a + D + Run C Code Download GitHub Binder In [1]: raise Nam

2 and 3

File Edit View Insert Cell Kernel Widgets Help Trusted Python 3 O In [4]: try:#creating a try catch block. In a try except bl

Add a comment
Know the answer?
Add Answer to:
(Python) Exercise-1: Raising Exceptions Try the following code one by one: raise NameError(‘Name not Found’) raise...
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
  • 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...

  • 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:...

  • use python IDEL Please highlight the answer 1 ווCIוסטIT Errors and Exceptions. There are two main...

    use python IDEL Please highlight the answer 1 ווCIוסטIT 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...

  • 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...

  • 3. Handling exceptions with a finally clause a. Download the following file from the class website:...

    3. Handling exceptions with a finally clause a. Download the following file from the class website: TestFinally.java b. Examine the code to determine what it does. c. Compile the code and notice the error. d. Modify TestFinally.java to handle the exception following these instructions (and those embedded in the code): i. Embedded in the code is a note showing the location for the try statement. ii. The first line of the catch block should look like this: catch(IOException e) {...

  • 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()...

  • Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing an...

    Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...

  • Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to...

    Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to do in the JAVA Language: Model your code from the Time Class Case Study in Chapter 8 of Java How to Program, Late Objects (11th Edition) by Dietel (except the displayTime method and the toUniversalString method). Use a default constructor. The set method will validate that the hourly employee’s rate of pay is not less than $15.00/hour and not greater than $30.00 and validate...

  • QUESTION 21 while True: , in Python, can be used to create an infinite loop. True...

    QUESTION 21 while True: , in Python, can be used to create an infinite loop. True False 2 points    QUESTION 22 The Python Framework does inform you where an error occurred True False 2 points    QUESTION 23 ____ is a critical component to being able to store data and information long term. File Access Memory Print function with 2 points    QUESTION 24 Error handling is also known as ___ handling Result Recursion Exception Crash 2 points   ...

  • 40pts) Given the following code segment. mSwer= input("Enter age: ") ile answer != 'q' : try:...

    40pts) Given the following code segment. mSwer= input("Enter age: ") ile answer != 'q' : try: print(int(answer) ) answer input ( " Enter age: ") except Value E rror : print ("Invalid answer 'q' age") Show what is printed if the user enters the following values at the prompt, one at a time. Print output User input 20 17 2a => 29 q 9. The following main function works with a class called Student that you will write. def 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