In today’s modern times, irrespective of the fact that if you are in the coding industry or not, you would probably have heard about Python at least once. Since its inception in 1991, this programming language has gathered a large amount of fame and valour thanks to its wide array of features as well as great versatility. But even so, there are some aspects of this programming language that continue to confuse both professionals as well as amateur programmers. One such aspect is that of print exceptions. Therefore, in this article we will explore Print Exception in Python and dive deep into its core.
Following pointers will be covered in this article,
- Errors in Python
- Syntax Errors vs Exceptions in Python
- Types of Exceptions in Python
- Raising an Exception
- Types of Exceptions in Python
Print Exception In Python
Errors in Python
In the Python operating system, there are two main types of errors. The first one is a syntax error and the second one is an exception error. No matter the type of error that you face in Python, once it pops up the whole interpreter will stop midway, thus disrupting your workflow. In this article we will focus on exceptions in Python and how you can make your way around them.
Moving on with this article on Print Exception In Python,
Syntax Errors vs Exceptions in Python
A syntax error in Python occurs when the interpreter detects an incorrect statement in your line of code. To understand this better, take a look at the example below.
>>> print( 0 / 0 )) File "<stdin>", line 1 print( 0 / 0 )) ^ SyntaxError: invalid syntax
The cursor in the above example indicates where exactly the syntax error lies in your code. In the above example, we used one too many brackets thus leading to a syntax error. Take a look at the correct example given below.
>>> print( 0 / 0) Traceback (most recent call last): File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
If you observe the above example, you will quickly realise that although there is no syntax error here, the interpreter has run into an exception error. What this basically means is that, by running your code the interpreter has produced an error, which is also known as an exception error.
Another thing to notice in the above example, is that the last line of the code indicates the type of exception error that is present in this line of code.
This is one of the most interesting aspects when it comes to Python. Instead of just telling you that there is an error in your code, the interpreter will go above and beyond to generate exceptions and tell you exactly what the error is. In some cases, if the error is new for the interpreter, it will create a new exception to conveniently define it for you.
Moving on with this article on Print Exception In Python,
Raising an Exception
In some cases you might need to manually raise an exception to help with the auditing purpose. In order to do this, make you use of the raise function. One advantage of using the raise function is that it can be complemented with a custom exception along with it. If in a certain situation, you want to include a certain condition along with the raise function, follow the example shared below.
x = 10
if x > 5:
raise Exception(‘x should not exceed 5. The value of x was: {}’.format(x))
When this code is run, the output will look something like this.
Traceback (most recent call last):
File “<input>”, line 4, in <module>
Exception: x should not exceed 5. The value of x was: 10
The program comes to a halt in between when the condition is met and an exception is shown on the screen.
Moving on with this article on Print Exception In Python,
Types of Exceptions in Python
In Python there are several types of exceptions that are available for one’s use. Some of the most significant ones are as mentioned below.
- The AssertionError Exception
- The else Clause
- The try and except Block
- The finally Exception
The AssertionError Exception
The AssertionError Exception is one of the most popular exceptions used by programmers worldwide. Instead of waiting for the program to halt midway, this method includes a condition at the beginning itself. If the condition is met, then the program continues running and if the condition is not met, then the program comes to a halt and an exception is raised on the screen. To understand this better, take a look at the example below.
import sys assert ('linux' in sys.platform), "This code runs on Linux only."
The else Clause
In Python you can use the else clause to run a certain block of code only if an exception is missing in the contents of the program. Take a look at the example below to understand this process better.
try: linux_interaction() except AssertionError as error: print(error) else: print('Executing the else clause.')
Moving on with this article on Print Exception In Python,
The try and except Block
The main purpose of the try and except Block in Python is to catch and handle exceptions. The interpreter follows the try statement and executes the program normally. In case there is an exception in the program, the statements after the except block are executed to handle them efficiently. Take a look at the example below to understand this concept better.
def linux_interaction(): assert ('linux' in sys.platform), "Function can only run on Linux systems." print('Doing something.') try: linux_interaction() except: pass
The finally Exception
In some situations, you will require the program to be executed no matter if you run into an exception or not. In these situations, the finally exception comes into action. By using this, you can urge the interpreter to keep running your code, no matter if the conditions are met or not. Take a look at the example below to understand this better.
try: linux_interaction() except AssertionError as error: print(error) else: try: with open('file.log') as file: read_data = file.read() except FileNotFoundError as fnf_error: print(fnf_error) finally: print('Cleaning up, irrespective of any exceptions.')
This brings us to the end of this article on Print Exception In Python,
To get in-depth knowledge on Python along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access. Got a question for us? Mention them in the comments section of this article and we will get back to you.