0% found this document useful (0 votes)
46 views

Errors Can Be of Various Types

Errors in programs can occur at different times and take different forms. Compile time errors like syntax errors are detected by the compiler. Runtime errors like logical errors cannot be detected until the program runs and may cause exceptions. Exceptions represent errors and disrupt normal program flow, and exception handling can make code more robust by guarding against failures. Common built-in Python exceptions include AttributeError, ImportError, and IndexError which are raised for attribute errors, import errors, and index errors respectively.

Uploaded by

Arya Bhatt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Errors Can Be of Various Types

Errors in programs can occur at different times and take different forms. Compile time errors like syntax errors are detected by the compiler. Runtime errors like logical errors cannot be detected until the program runs and may cause exceptions. Exceptions represent errors and disrupt normal program flow, and exception handling can make code more robust by guarding against failures. Common built-in Python exceptions include AttributeError, ImportError, and IndexError which are raised for attribute errors, import errors, and index errors respectively.

Uploaded by

Arya Bhatt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Errors can be of various types:

From the point of view of when errors are detected, we distinguish:

 Compile time errors: syntax errors and static semantic errors indicated by the compiler.

 Runtime errors: dynamic semantic errors, and logical errors, that cannot be detected by the compiler
(debugging).

Syntax Error
Syntax errors often called as parsing errors, are predominantly caused when the parser detects a syntactic issue
in your code.

Semantic Errors
Semantic errors are improper uses of “program statements”, we are saying here that logic errors produce
wrong data while semantic errors produce nothing meaningful at all.

Python Logical Errors (Exceptions)


Errors that occur at runtime (after passing the syntax test) are called exceptions or logical errors. For instance,
they occur when we try to open a file(for reading) that does not exist (FileNotFoundError), try to divide a
number by zero (ZeroDivisionError), or try to import a module that does not exist (ImportError).

What is an Exception?

An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the
program's instructions. In general, when a Python script encounters a situation that it cannot cope with, it raises
an exception. An exception is a Python object that represents an error.

Why use Exceptions?

Many a time, there are valid as well as invalid exceptions. Exceptions are convenient in many ways for handling
errors and special conditions in a program. When you think that you have a code which can produce an error,
you can use exception handling technique.
Error handling increases the robustness of your code, which guards against potential failures that would cause
your program to exit in an uncontrolled fashion.

Some of the common built-in exceptions in Python programming along with the error that cause them are listed
below:

Exception Cause of Error

AssertionError Raised when an assert statement fails.

AttributeError Raised when attribute assignment or reference fails.

EOFError Raised when the input() function hits end-of-file condition.

FloatingPointError Raised when a floating point operation fails.

GeneratorExit Raise when a generator's close() method is called.

ImportError Raised when the imported module is not found.

IndexError Raised when the index of a sequence is out of range.

KeyError Raised when a key is not found in a dictionary.

KeyboardInterrupt Raised when the user hits the interrupt key (Ctrl+C or Delete).

MemoryError Raised when an operation runs out of memory.

NameError Raised when a variable is not found in local or global scope.

NotImplementedError Raised by abstract methods.

OSError Raised when system operation causes system related error.

Raised when the result of an arithmetic operation is too large to


OverflowError
be represented.

Raised when a weak reference proxy is used to access a garbage


ReferenceError
collected referent.

RuntimeError Raised when an error does not fall under any other category.

Raised by next() function to indicate that there is no further item


StopIteration
to be returned by iterator.
SyntaxError Raised by parser when syntax error is encountered.

IndentationError Raised when there is incorrect indentation.

TabError Raised when indentation consists of inconsistent tabs and spaces.

SystemError Raised when interpreter detects internal error.

SystemExit Raised by sys.exit() function.

Raised when a function or operation is applied to an object of


TypeError
incorrect type.

Raised when a reference is made to a local variable in a function


UnboundLocalError
or method, but no value has been bound to that variable.

Raised when a Unicode-related encoding or decoding error


UnicodeError
occurs.

UnicodeEncodeError Raised when a Unicode-related error occurs during encoding.

UnicodeDecodeError Raised when a Unicode-related error occurs during decoding.

UnicodeTranslateErro
Raised when a Unicode-related error occurs during translating.
r

Raised when a function gets an argument of correct type but


ValueError
improper value.

Raised when the second operand of division or modulo operation


ZeroDivisionError
is zero.

If required, we can also define our own exceptions in Python.

Raising Exceptions in Python

You can raise an exception in your program by using the raise exception statement. Raising an exception breaks
current code execution and returns the exception back until it is handled.

You might also like