Understanding Python Exceptions

Introduction

In any programming language, encountering exceptions (errors) is inevitable. Knowing how to view errors, understand them, avoid them, and capture error information are all aspects we need to address.

1. Definition of Exceptions

An exception occurs when the <span>Python</span> interpreter detects an error and can no longer continue execution. At this point, some error messages will appear, which are referred to as “exceptions,” commonly known as <span>BUG</span>s. For example, a common bug is a typo in a function name:

pring("Biomamba")
# This will return an error and indicate the location and reason for the error

NameError: name ‘pring’ is not defined

2. Exception Handling

When a program encounters a <span>bug</span>, there are two possible outcomes: 1. Terminate the program. 2. Notify the occurrence of the <span>bug</span> and allow the program to continue running. The second situation requires <span>exception handling</span> to prevent the entire program from stopping due to minor bugs. The basic syntax for exception handling is:<span>try:</span> code that may cause an error <span>except:</span> code to execute if an exception occurs

# Catching exceptions (default does not distinguish exception types):
try:
    prinf('This line of code may have an exception')
except:
    print("The previous line of code had an exception, this line is output")
# As we can see, the above lines of code did not throw an error, and the output is the statement after except
## The previous line of code had an exception, this line is output
# Catching specific exceptions
# For example, here we catch the undefined variable exception "NameError"
try:
    print(Biomamba)
except NameError as aerror:
    print("Biomamba is not defined")
# We can see that the error was successfully caught, and the alternative solution was executed
## Biomamba is not defined
# Catching specific exceptions
# Here we let the exception not belong to NameError:
try:
    print(1/0)
except NameError:
    print("Biomamba is not defined")
# We can see that the error was not caught, the prompt information was output, and the alternative code was not executed

ZeroDivisionError: division by zero

# Catching multiple exceptions, except (exception1, exception2)
try:
    print(1/0)
except (NameError, ZeroDivisionError):
    print("This is the result of the alternative code execution")
# We can see that the alternative code was successfully executed
## This is the result of the alternative code execution
# Catching all types of exceptions
try:
    print(1/0)
    print(Biomamba)
except Exception:
    print("This is the result of the alternative code execution")
# We can see that the alternative code was successfully executed

<span>## This is the result of the alternative code execution</span>

3. Exception Else

In addition to <span>except</span>, you can also set code to execute when no exceptions occur:

try:
    print("No exceptions here")
except:
    print("This is the result after an exception")
else:
    print("This is the result when no exceptions are caught")
# Everyone can experience the execution logic of the above lines of code
## No exceptions here
## This is the result when no exceptions are caught

4. Exception Finally

Regardless of whether an exception occurs, the statements contained in <span>finally</span> will always be executed:

# With exception
try:
    1/0
    print("No exceptions here")
except:
    print("This is the result after an exception")
else:
    print("This is the result when no exceptions are caught")
finally:
    print("I will be executed under all conditions")
# Everyone can experience the execution logic of the above lines of code
## This is the result after an exception
## I will be executed under all conditions
# Without exception
try:
    1+1
    print("No exceptions here")
except:
    print("This is the result after an exception")
else:
    print("This is the result when no exceptions are caught")
finally:
    print("I will be executed under all conditions")
# Everyone can experience the execution logic of the above lines of code
## 2
## No exceptions here
## This is the result when no exceptions are caught
## I will be executed under all conditions

5. Exception Propagation

Exceptions are propagable and can be defined using as to receive the exception variable:

try:
    1/0
except Exception as my_error:
    print("Exception successfully caught, here is the specific information about the exception:
")
    print(my_error)
# We can see that the division by zero exception was successfully caught
## Exception successfully caught, here is the specific information about the exception:
## 
## division by zero
# However, the captured exception is a local variable, and after exiting the exception handling statement, the captured exception variable becomes invalid:
try:
    1/0
except Exception as my_error:
    print("Exception successfully caught, here is the specific information about the exception:
")
    print(my_error)
# We can see that the division by zero exception was successfully caught, but the my_error variable does not exist

NameError: name ‘my_error’ is not defined

Previous Reviews

Bioinformatics Python Quick Reference

Python Installation (Windows + Linux)

Python’s “Rstudio” – Pycharm

Python Tool: Jupyter Notebook

Understanding Python Basics: Literals, Comments, Variables, Types, Operators

Python Conditional Statements

Python Loop Statements

Python Functions and Methods

Mastering Python Data Containers

Advanced Python Functions

Python File Operations

Understanding Python Exceptions

Contact Us

Understanding Python ExceptionsUnderstanding Python ExceptionsLeave a message to receive materials andready-to-use single-cell analysis images atWeChat ID[Biomamba_zhushou], making it convenient for everyone to communicate at any time. We have also built a group chat matrix, and everyone is welcome to join the discussion.After reading these articles, feel free to addtips for beginners in bioinformaticsWithout searching, there is no right to speak

Students who already have contact information for the bioinformatics basedo not need to add again

Understanding Python Exceptions

Understanding Python ExceptionsThe likes and views you give are taken seriously as appreciation

Leave a Comment