coderz.py

Keep Coding Keep Cheering!

Errors and Built-in Exceptions

JOIN Clause

A minor typing error can result in an error in any programming language as we must follow the syntax rules when coding in any programming language.

Python Errors:

We can make mistakes when writing a program that causes errors when we run it. When a Python program encounters an unhandled error, it crashes. An exception is a Python object that represents an error. The various types of Python errors are broadly classified as follows:

  • Syntax errors
  • Logic errors
Syntax Errors:

This is the most common and basic type of error scenario. The most common cause of an error in a Python program is the incorrect use of a statement. A syntax error, also known as a parsing error, is a type of error like this. The Python interpreter reports it quickly, usually with an explanation.

Example:

print"Hello world"

Output:

File "<string>", line 1
    print"Hello world"
         ^
SyntaxError: invalid syntax

Example:

a = 10
if a < 20
    print('Value of a less than 20')

Output:

File "<string>", line 3
    if a < 20
            ^
SyntaxError: invalid syntax
 What is an Exception?

In contrast to syntax errors, exceptions are types of errors that occur as a result of code malfunctioning during execution. Exceptions are errors that occur during execution. In Python, all the exception classes are derived from the BaseException class.

Example:

print(5/0)

Output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ZeroDivisionError: division by zero

It can be seen that dividing five by zero results in ZeroDivisionError, which is a child of the ArithmeticError class.

Python Exception Message Decoding:
  • Traceback: In the exception message, the term Traceback means that Python has traced back the code to the point where the exception occurred and will show the related messages after this line.
  • File “<string>”, line 1, in <module>: this line tells us the name of the python file and the exact line number for the code due to which the exception was generated.
  • ZeroDivisionError: This is the type of exception that has occurred.
In-built Python Exception:

Now that we understand what an exception is, let’s look at a list of Python exceptions. Try to recall if you’ve ever encountered any of these Python exceptions as you read the list.

ExceptionCause of Error
AssertionErrorWhen an assert statement fails, an error is raised.
ArithmeticErrorIt specifies when a numerical calculation error occurs.
AttributeErrorThis exception is thrown when an attribute assignment or reference fails.
EOFErrorWhen the input() method comes across an “end of file” situation, it throws an error (EOF)
FloatingPointErrorWhen a floating-point calculation fails, this error is generated.
GeneratorExitWhen the close() method of a generator is called, this variable is raised.
ImportErrorWhen the imported module cannot be found, this exception is thrown.
IndexErrorWhen the index of a sequence is out of range, this value is raised.
KeyErrorWhen a key is not found in a dictionary, this error is raised.
KeyboardInterruptWhen the user pushes Ctrl+C, Ctrl+Z, or Delete, this exception is thrown.
MemoryErrorWhen a program runs out of memory, an error is generated.
NameErrorWhen a variable is not discovered in the local or global scope, this exception is raised.
NotImplementedErrorRaised through abstract methods.
OSErrorWhen a system operation results in a system-related error, this flag is raised.
OverflowErrorWhen the result of a numerical calculation is too huge, an error is raised.
ReferenceErrorThis Is an Exception When a weak reference object does not exist, an error is raised.
RuntimeErrorWhen an error does not fit into any of the other categories, it is raised.
StopIterationRaised by the next() function to indicate that the iterator has no more items to return.
SyntaxErrorWhen a syntax problem occurs, the parser raises this exception.
IndentationErrorIt raises an error when the indentation is incorrect.
TabErrorIt raises an error when the indentation consists of tabs or spaces.
SystemErrorIt is triggered when a system error occurs.
SystemExitThe sys.exit() function raised this exception.
TypeErrorWhen a function or operation is applied to an object of the wrong type, this exception is raised.
UnboundLocalErrorWhen a reference to a local variable in a function or method is made but no value is bound to that variable, an exception is raised.
UnicodeErrorWhen a Unicode-related encoding or decoding problem occurs, this flag is raised.
UnicodeEncodeErrorWhen a Unicode-related problem occurs during encoding, this flag is raised.
UnicodeDecodeErrorWhen a Unicode-related error occurs while decoding, this flag is raised.
UnicodeTranslateErrorIt defines an error. When there is an issue with a Unicode translation, this flag is raised.
ValueErrorWhen there is an incorrect value in a specified data type, this exception is raised.
ZeroDivisionErrorWhen the second operator in a division is zero, an error is raised.

If necessary, we can even define our own exceptions in Python. We can manage these built-in and user-defined exceptions in Python using try, except, and finally statements.

Note: also read about Static Keyword in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement