Best writers. Best papers. Let professionals take care of your academic papers

Order a similar paper and get 15% discount on your first order with us
Use the following coupon "FIRST15"
ORDER NOW

Errors

Question 1: Errors

It is often said that nothing in life is certain but death and taxes. For a programmer or data scientist, however, nothing is certain but encountering errors.

In Python, there are two primary types of errors, both of which you are likely familiar with: syntax errors and exceptions. Syntax errors occur when the proper structure of the language is not followed, while exceptions are errors that occur during the execution of a program. These include errors such as ZeroDivisionError, TypeError, NameError, and many more!

Under the hood, these errors are based in the concepts of object orientation, and all exceptions are class objects. If you’re interested in more detailed explanations of the structure of exceptions as well as how to create one, check out this article from the Python documentation! In the meantime, we’ll implement our own version of an Error class

Complete the Error, SyntaxError, and ZeroDivisionError classes such that they create the correct messages when called.

  • The SyntaxError and ZeroDivisionError classes inherit from the Error class and add functionality that is unique to those particular errors. Their code is partially implemented for you.
  • The add_code method adds a new helpful message to your error, while the write method should print the output that you see when an error is raised.
  • You can access the parent class methods using the super() function
class Error:
    """
    >>> err1 = Error(12, "error.py")
    >>> err1.write()
    error.py:12
    """
    def __init__(self, line, file):
        "*** YOUR CODE HERE ***"

    def format(self):
        return self.file + ':' + str(self.line)

    def write(self):
        print(self.format())

class SyntaxError(Error):
    """
    >>> err1 = SyntaxError(17, "lab10.py")
    >>> err1.write()
    lab10.py:17 SyntaxError : Invalid syntax
    >>> err1.add_code(4, "EOL while scanning string literal")
    >>> err2 = SyntaxError(18, "lab10.py", 4)
    >>> err2.write()
    lab10.py:18 SyntaxError : EOL while scanning string literal
    """
    type = 'SyntaxError'
    msgs = {0 : "Invalid syntax", 1: "Unmatched parentheses", 2: "Incorrect indentation", 3: "missing colon"}

    def __init__(self, line, file, code=0):
        "*** YOUR CODE HERE ***"

    def format(self):
        "*** YOUR CODE HERE ***"

    def add_code(self, code, msg):
        "*** YOUR CODE HERE ***"

class ZeroDivisionError(Error):
    """
    >>> err1 = ZeroDivisionError(273, "lab10.py")
    >>> err1.write()
    lab10.py:273 ZeroDivisionError : division by zero
    """
    type = 'ZeroDivisionError'

    def __init__(self, line, file, message='division by zero'):
        "*** YOUR CODE HERE ***"

    def format(self):
        end = self.type + ' : ' + self.message
        "*** YOUR CODE HERE ***"
 
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"