All You Need To Know About Exceptions And Errors In Python
Exceptions and errors are a normal part of programming, and they occur when something goes wrong during the execution of a program.
In Python, exceptions and errors are represented by special objects called "exceptions", and you can use them to handle and respond to errors in your code.
There are several types of exceptions in Python, and each one indicates a different type of error.
For example, the ZeroDivisionError
exception is raised when you try to divide a number by zero, and the TypeError
exception is raised when you try to perform an operation on an object of an unsupported type.
To handle exceptions in Python, you use the try
and except
keywords, like so:
try:
# code that might cause an exception
except ExceptionType:
# code to handle the exception
In the example above, the code in the try
block is executed, and if an exception is raised, the code in the except
block is executed.
You can also specify multiple except
blocks to handle different types of exceptions, like so:
try:
# code that might cause an exception
except ExceptionType1:
# code to handle ExceptionType1
except ExceptionType2:
# code to handle ExceptionType2
You can also use the else
keyword to specify a block of code that should be executed if no exceptions are raised in the try
block, like so:
try:
# code that might cause an exception
except ExceptionType:
# code to handle the exception
else:
# code to be executed if no exceptions are raised
Finally, you can use the finally
keyword to specify a block of code that should be executed regardless of whether an exception is raised or not, like so:
try:
# code that might cause an exception
except ExceptionType:
# code to handle the exception
finally:
# code to be executed regardless of whether an exception is raised or not
It is important to note that you should only use exceptions to handle exceptional situations, and not as a way to control the flow of your program.
For example, it is not a good idea to use exceptions to handle invalid input from the user, because it is a common and expected occurrence.
Instead, you should use proper input validation and error handling techniques to handle this situation.
Here is an example of how you might use exceptions to handle a simple division operation:
def divide(x, y):
try:
result = x / y
except ZeroDivisionError:
print("Cannot divide by zero!")
else:
print("Result is", result)
finally:
print("Executing finally clause")
divide(10, 2)
divide(10, 0)
Output
Result is 5
Cannot divide by zero!
In the above example, we have defined a divide
function that
takes two numbers as arguments and divides the first one by the second.
If the second number is zero, it raises a ZeroDivisionError
exception, which is caught by the except
block and handled appropriately.
If no exceptions are raised, the else
block is executed, and the result of the division is printed.
Finally, the finally
block is always executed, regardless of whether an exception is raised or not.
Reference Books
Here are the books I’ve used as references for writing this article,
please feel free to read them If you don’t want your knowledge to be
limited to this article alone.
Post a Comment