avatarYang Zhou

Summary

The provided web content discusses the handling of exceptions in Python programming, emphasizing the importance of robust code that can manage errors and unexpected situations using Python's built-in exception handling mechanisms.

Abstract

The article "Exceptions Handling in Python" explains the necessity of managing errors beyond just debugging code. It distinguishes between bugs, which are coding errors, and exceptions, which are runtime errors that may arise from external circumstances such as a full disk or a broken network connection. The author highlights Python's exception handling structure, which includes try, except, else, and finally blocks, to create more resilient programs. Through code examples, the article demonstrates how to catch and handle different types of exceptions, such as ValueError and ZeroDivisionError, and stresses the significance of the finally block for executing code that must run regardless of whether an exception occurred. The article also directs readers to Python's built-in error objects and encourages following the author for more Python tutorials.

Opinions

  • The author suggests that a robust program should not only be free of bugs but also capable of handling exceptions effectively.
  • Different types of errors should be handled with corresponding except blocks to ensure appropriate responses to different error conditions.
  • The finally block is presented as a crucial tool for guaranteeing the execution of cleanup actions, such as closing files, which should occur regardless of any exceptions.
  • The article implies that understanding and implementing Python's exception handling mechanisms lead to better and more professional code.
  • By providing examples and linking to the Python documentation, the author conveys a commitment to educating readers and promoting best practices in Python programming.
  • The encouragement to follow the author for more tutorials indicates an ongoing dedication to supporting the Python programming community with educational content.

Exceptions Handling in Python

Photo by Nathan Dumlao on Unsplash

Introduction

In the process of programming, we will always encounter various errors. Some errors are caused by our code. Such as syntax error. This kind of error is usually called a bug. The bug must be fixed.

However, a bug-free program still may occur problems. Because some errors are not caused by our code. They may be caused by accidental situations or operations. This type of error is called exceptions. For example:

  • While writing data to files, the disk may be full and cannot be written in.
  • When some data is being downloaded, the network is suddenly broken.
  • When dividing, enter 0 as the denominator.
  • … …

The robust code is not only bug-free but also handling exceptions well.

Exceptions Handling Structure in Python

Fortunately, Python has a built-in exception handling mechanism to help us handle exceptions conveniently. It is the try...except...finally mechanism. Let’s see an example:

As the above example shown, when we consider some part of code may occur exceptions, we can put them into the try statement. If exceptions really happen, the code in the except statement block will run. Finally, the finally statement will run no matter the exceptions happened or not.

There should be many types of errors. If different types of errors occur, they should be handled by different except blocks. Of course, there can be multiple except to catch different types of errors. As to our example, how to deal with wrong inputs that are not numbers?

As we can see, if we entered a string as a numerator, there raised a ValueError . Therefore, our code is more robust since it can handle two types of exceptions 😄.

The ValueError and ZeroDivisionError are Python’s build-in error objects. The full list of Python error objects can be found in Python docs.

In addition, we can add an else block following the except block. It will run if there is no exception happened.

Note: The else block will run when there is no exceptions happened. The finally block will definitely run no matter what happened.

Why We Need the Finally Block?

As mentioned above, the finally block will run anyway. When we need to make sure something will be done no matter meets exceptions or not, this feature is very useful. For instance, when we open a file and do some operations on it, we should make sure we will close the file finally.

The following examples show what’s the difference between writing code in a finally block and writing code outside it.

As the example4.py shown, even we meet the ZeroDivisionError and there is an exit() in its block, the code in the finally block will still run (print “The End”).

How about taking the finally keyword away?

The above program has already exited before running the print('The End')! There is no “The End” message will be printed.

Thanks for reading, if you like it, don’t forget to follow me and enjoy more great Python tutorials:

Programming
Python
Python3
Python Programming
Recommended from ReadMedium