Error and Exception Hadling

https://www.geeksforgeeks.org/python-exception-handling/

1Error

Error in Python can be of two types i.e. Syntax errors and Exceptions. Errors are the problems in a program due to which the program will stop(terminate) the execution. On the other hand, exceptions are raised when some internal events occur which changes the normal flow of the program.

2Difference between Syntax Error and Exceptions

Syntax Error: As the name suggests this error is caused by the wrong syntax in the code. It leads to the termination of the program.

amount = 10000 if(amount > 2999) print("You are eligible to purchase Dsa Self Paced") Output: if(amount > 2999) ^ SyntaxError: invalid syntax

Exceptions: Exceptions are raised when the program is syntactically correct, but the code resulted in an error. This error does not stop the execution of the program, however, it changes the normal flow of the program

marks = 10000 a = marks / 0 print(a) Output: zeroDivisionError: division by zero

2Type of Exception Error
ExceptionDescription
IndexErrorWhen the wrong index of a list is retrieved.
ZeroDivisionErrordivision by zero
IndentationErrorexpected an indented block
AssertionErrorIt occurs when the assert statement fails
AttributeErrorIt occurs when an attribute assignment is failed.
ImportErrorIt occurs when an imported module is not found.
KeyErrorIt occurs when the key of the dictionary is not found.
NameErrorIt occurs when the variable is not defined.
MemoryErrorIt occurs when a program runs out of memory.
TypeErrorIt occurs when a function and operation are applied in an incorrect type.
X