=================================================================================
In Python, try and except are keywords used for exception handling, which allows you to handle errors or exceptions that may occur during the execution of a program. The basic idea is to enclose a block of code within a try block, and if any exceptions occur within that block, they can be caught and handled in the corresponding except block.
The primary purpose of the final block in try-except statements is that it contains clean-up code which will always be executed.
This block lets you test a block of code for errors, for instance, this code. A more official, but simple example is code:

In this script above, the try block contains code that may raise an exception. If an exception occurs, it will be caught by the corresponding except block. We can have multiple except blocks to handle different types of exceptions. The else block is optional and will be executed if no exception occurs. The finally block is also optional and will be executed regardless of whether an exception occurs or not.
============================================
Common examples of exception:
Division by Zero
Accessing a file which does not exist.
Addition of two incompatible types
Trying to access a nonexistent index of a sequence
Removing the table from the disconnected database server
ATM withdrawal of more than the available amount
Exception handling is managed by the following 5 keywords:
try
catch
finally
throw
For instance, some methods can be used to handle such no results:
if results:
if results is not None:
if len(results)!=0:
Try/Exception
Table 4568a. Important Python errors.
Error Type |
Description |
| ArithmeticError |
Act as a base class for all arithmetic exceptions. It is raised for errors in arithmetic operations. |
| ImportError |
Is raised when you are trying to import a module which does not present. This kind of exception occurs if you have made typing mistake in the module name or the module which is not present in the standard path. |
| IndexError |
Is raised when you try to refer a sequence which is out of range. |
| KeyError |
When a specific key is not found in a dictionary, a KeyError exception is raised. |
| NameError |
Is raised when a name is referred to in code which never exists in the local or global namespace. |
| ValueError |
Is raised when a function or built-in operation receives an argument which may be of correct type but does not have suitable value. |
| EOFerror |
This kind of error raises when one of the built-in functions (input() or raw_input()) reaches an EOF condition without reading any data. |
| ZeroDivisonError |
This type of error raised when division or module by zero takes place for all numeric types. |
| AttributeError |
It occurs when you try to access an attribute or method of an object that does not exist. |
| IOError- |
Is raised when an input/output operation fails. (code) |
| syntaxError |
Is raised when there is an error in Python syntax. |
| IndentationError |
Is raised when indentation is not properly defined |
Table 4568b. Important Python exceptions.
Exception |
Description |
Examples |
| ArithmeticException |
Arithmetic error, such as divide-by-zero. |
|
| ArraylndexOutOfBoundsException |
Array index is out-of-bounds |
|
| ArrayStoreException |
Assignment helps you to the array element of an incompatible type. |
|
| ClassCastException |
Invalid cast |
|
| MlegalMonitorStateException |
Illegal monitor operation, like waiting on an unlocked thread. |
|
| MlegalStateException |
Environment or application is in wrong state. |
|
| ClassNotFoundException |
Class not found |
|
| CloneNotSupportedException |
Attempt to clone an object which does not implement the Cloneable interface. |
|
| Illegal AccessException |
Access to a class is denied. |
|
| InstantiationException |
Occurs when you attempt to create an object of an interface or abstract class. |
|
| CloneNotSupportedException |
Attempt to clone an object which does not implement the interface. |
|
| TypeError |
It is an exception that occurs when you perform an operation on an object of an inappropriate type. |
(Exception for locateCenterOnScreen). |
Table 4568c. Error vs. Exceptions.
Error |
Exceptions |
| All errors in Python are the unchecked type. |
Exceptions include both checked and unchecked type. |
| Errors occur at run time which unknown to the compiler. |
Exceptions can be recover by handling them with the help of try-catch blocks. |
| Errors are mostly caused by the environment in which an application is running. |
The application itself causes exceptions.
Examples: |
============================================
except ValueError and ZeroDivisionError. code:

Output:

|