Errors and Exceptions in P

 Errors and Exceptions

·         Errors are the problems/mistakes/bugs in a program that will stop the execution of the program.
·         It is raised due to some internal events that occur and change the flow of the normal program.
· The process of finding and eliminating errors is called debugging.

 Types of Errors

Errors can be classified into three major groups:

  • ·         Syntax errors
  • ·         Runtime errors
  • ·         Logical errors
  • ·         Out of Memory Error
  • ·         Keyboard Interrupt Error

 Syntax errors (parsing errors)

·         Syntax errors are mistakes in the use of the Python language.
·         It is due to spelling or grammar mistakes in a language like English.

 Common Python syntax errors include:

  • ·         Leaving out a keyword
  • ·         Putting a keyword in the wrong place
  • ·         Leaving out a symbol, such as a colon, comma, or brackets
  • ·         Misspelling a keyword
  • ·         Incorrect indentation
  • ·         Empty block

 Examples:

>>> while True print('Hello world')
  File "<stdin>", line 1
    while True print('Hello world')
                   ^
SyntaxError: invalid syntax

 In the example, the error is detected at the function print(), since a colon (':') is missing before it.

 

Runtime errors

·         If a program is syntactically correct it will be run by the Python interpreter exit program unexpectedly during execution so it is called a runtime error.

 Examples of Python runtime errors:

  • ·         Division by zero
  • ·         Performing an operation on incompatible types
  • ·         Using an identifier that has not been defined
  • ·         Accessing a list element, dictionary value, or object attribute that doesn’t exist
  • ·         Trying to access a file which doesn’t exist

Example

# initialize the amount of variable

marks = 10000

  # perform division with 0

a = marks / 0

print(a)

Output:

Logical errors (Exception)

  • ·         Logical errors are the most difficult to fix.
  • ·         In this error the program runs without crashing but produces an incorrect result.
  • ·         This error is created due to mistake in the program’s logic.
  • ·         No syntax or runtime error has occurred in this error.

Some reasons that generate logical errors:

  • ·         Using the wrong variable name
  • ·         Indenting a block to the wrong level
  • ·         Using integer division instead of floating-point division
  • ·         Getting operator precedence wrong
  • ·         Making a mistake in a boolean expression
  • ·         Off-by-one, and other numerical errors

 

Out-of-Memory Error

  • Memory errors are mostly dependent on system RAM and are related to Heap.
  • If large objects (or) referenced objects in memory, then it will show OutofMemoryError (Source).
  • It can be caused due to various reasons:
  • ·         Using a 32-bit Python Architecture (Maximum Memory Allocation given is very low, between 2GB - 4GB).
  • ·         Loading a very large data file
  • ·         Running a Machine Learning/Deep Learning model and many more.

 Handle the memory error by exception handling, a fallback exception for when the interpreter entirely runs out of memory and must immediately stop the current execution.

Python adopts the memory management architecture of the C language (malloc() function), it is not certain that all processes of the script will recover — in some cases, a MemoryError will result in an unrecoverable crash.

 Keyboard Interrupt Error

The Keyboard Interrupt exception is raised when you try to stop a running program by pressing ctrl+c or ctrl+z in a command line

==================================================== 


Post a Comment

0 Comments