Python Devlopment






Exceptions



An exception is an error that happens during execution of a program. When that error occurs, Python generate an exception that can be handled, which avoids your program to crash

 

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.

 

When a Python script raises an exception, it must either handle the exception immediately otherwise it terminates and quits.

 

While running a program, we often end up making some errors. There are many tyoes of errors that can occurs in a program. The error caused by writing an improper syntax is called syntax error or parsing error, these are also called compile time errors.

 

Error can also occurs at runtime and these runtime errors are called exceptions. 

Example:

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.

 

When a Python script raises an exception, it must either handle the exception immediately otherwise it terminates and quits.

Example:

 

#Compile Time Error
a=10
if(a<20)
    print("a is less than 20")

SyntaxError: invalid syntax

The error shown an=bove is syntax error because there is a problem with the syntax; the if statement doesn't end with colon.

 

ZeroDivisionError

>>> 5/0

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in 
    5/0
ZeroDivisionError: division by zero

here we tried to divide 5 by 0. As a result, the interpreter prints  ZeroDivisionError

 

1. Built-in Exceptions

Any error prone statements can raise exception. There are various built-in exceptions in Python that can be raised when the corresponding error occurs. Followings are the some of the built-in exceptions.

1. AssertionError : Raised when assert statement fails.

2. AttributeError : Raised when attribute assignment or reference fails.

3. EOFError : Raised when the input() functions hits end-of-file condition.

4. FloatingPointError : Raised when a floating point operation fails.

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

6. ImportError : Raised when the imported module is not found.

7. IndexError : Raised when index of a sequence is out of range.

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

9. KeyboardInterrupt : Raised when the user hits interrupt key (Ctrl+c or delete).

10. MemoryError : Raised when an operation runs out of memory.

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

12. NotImplementedError : Raised by abstract methods.

13. OSError :Raised when system operation causes system related error.

14. OverflowError :Raised when result of an arithmetic operation is too large to be represented.

15. ReferenceError : Raised when a weak reference proxy is used to access a garbage collected referent.

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

17. SyntaxError : Raised by parser when syntax error is encountered. 

18. IndentationError : Raised when there is incorrect indentation.

19. TabError : Raised when indentation consists of inconsistent tabs and spaces.

20. SystemError : Raised when interpreter detects internal error.

21. SystemExit : Raised by sys.exit() function.

22. TypeError: Raised when a function or operation is applied to an object of incorrect type.

23. UnicodeError : Raised when a Unicode-related encoding or decoding error occurs.

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

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

26. UnicodeTranslateError : Raised when a Unicode-related error occurs during translating.

27. ValueError : Raised when a function gets argument of correct type but improper value.

28. ZeroDivisionError : Raised when second operand of division or modulo operation is zero.