https://www.geeksforgeeks.org/python-exception-handling/
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.
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
| Exception | Description |
|---|---|
| IndexError | When the wrong index of a list is retrieved. |
| ZeroDivisionError | division by zero |
| IndentationError | expected an indented block |
| AssertionError | It occurs when the assert statement fails |
| AttributeError | It occurs when an attribute assignment is failed. |
| ImportError | It occurs when an imported module is not found. |
| KeyError | It occurs when the key of the dictionary is not found. |
| NameError | It occurs when the variable is not defined. |
| MemoryError | It occurs when a program runs out of memory. |
| TypeError | It occurs when a function and operation are applied in an incorrect type. |