Error in Python

Posted on

Introduction

Actually, in this article the main purpose is to be able to describe for a specific purpose. That specific purpose is for recognizing error upon program using Python programming language execution. Specifically, it is for detecting the running program so that it will be forced to stop whenever that running program is facing error in the execution process. Furthermore, those errors can be classified into two main groups. The first one is an error syntax and the second one is an error exception

Error Syntax in Python

An error syntax is the most fundamental error exist as part of the program. In Python programming language, it is the most common and the most easy type of error. An example of the error syntax  is miss-placed for the indentation of the script. Another most common and most easy one is forgetting to place double colon to initialize a block. For an example, the following is an error syntax by forgetting to add double colon for initializing a block :

for letter in 'I am a statement'
  print(letter)

From the above source code, the following is the execution which will cause a syntax error as it exist below :

>>> for letter in 'I am a statement
File "<stdin>", line 1
for letter in 'I am a statement
^
SyntaxError: unterminated string literal (detected at line 1)
>>>

Error Exception in Python

Another error type which exist in Python programming language is the error exception. Error exception is an error when the code has a mistake but it has another error, that is why it will be called as exception. Few samples of exception which is owned by Python programming language :

  1. ZeroDivisionError

    In this context, the Python programming language’s ZeroDivisionError” usually occurs when there is a process as an attempt to try dividing number by 0. Below is an example of the execution of Python’s source code in the Python command console :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\>python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a = 10
    >>> b = 0
    >>> a/b
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ZeroDivisionError: division by zero
    >>>
    
  2. ValueError

    In this case, for the Python programming language’s “ValueError”  appear when a function is receiving an argument with inappropriate value. In other words, the type is correct but the value is wrong. Basically, as the name of the error which is “ValueError”, the trigger is using wrong value in the process. As an example, the following Python programming language’s source code execution in the Python command console to show the “ValueError” :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\>python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import math
    >>> math.sqrt(-4);
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: math domain error
    >>>
    
  3. AttributeError

    As for the “AttributeError” For  in Python programming language, the trigger is when there is an invalid execution in the attribute reference. Generally, a variable can have an attribute for further execution. So, as it exist as part of the name “AttributeError”, the trigger is using wrong attribute in the process. In other words, if the attribute is not exist, it will generate the “AttributeError”.

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\>python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    
    >>> list = [1,2,3,4,5]
    >>> list.add(5);
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: 'list' object has no attribute 'add'
    >>>
    

Leave a Reply