piton

Python Throw Exception

Python Throw Exception

An exception appears during program execution and changes its normal flow due to an error. An exception arises on account of an error. The main cause of an exception is a logical error. Like many other programming languages, Python provides several built-in exceptions, i.e., ZeroDivisionError, ImportError, EOFError, etc.; for instance, the ZeroDivisionError exception is raised when a number is divided by zero. The Python exceptions are handled by the try statement. We define a try block and put the code vulnerable code inside this block, which can raise an exception. Next, after the try block, we define an except block to deal with the exception. Besides all these built-in exceptions, sometimes we need to raise or throw an exception when we encounter a specific situation. The Python developer can throw a user-defined exception easily. We use the raise keyword for raising or throwing an exception. This article explains the Python raise keyword usage for throwing the exception with examples.

Syntax

The syntax for throwing an exception is very straightforward, and as follows:

Raise Exception(“any message”)

After writing the raise keyword, define your exception.

Exception handling example

First, let's see an example of a try-except block that how we can deal with the Python built-in exception, and after this, we will see some examples of throwing or raising Python exceptions. In the example given below, we have created two variables. The second variable's value is equal to zero. When we divide the num1 with num2, it will raise a “ZeroDivisionError”. The division code will throw an exception; therefore, it is placed inside the try block. The except block catches the exception and prints the message “An unexpected error occurred”.

#declaring a number variable
num1 =20
#declaring second number variable
num2 =0
#implementing the try block
try:
result=num1/num2
except:
print("An unexpected error occurred")

Output

Raise an exception example

Now let's understand through examples that how we can throw or raise an exception by using the raise keyword. In the given example, we are raising an exception when a number is divided by any negative number.

#declaring a number variable
num1 =20
#declaring second number variable
num2 =-10
if(num2<0):
#raising an exception
raise Exception("The number 2 should not be a negative number")
else:
result=num1/num2
print(result)

Output

The output shows that the exception “The number 2 should not be a negative number” is raised.

We can also define the type of error. Let's have a look at it.

#declaring a number variable
num1 =20
#declaring second number variable
num2 =-10
if(num2<0):
#raising an exception
raise TypeError("The negative number error")
else:
result=num1/num2
print(result)

Output

In the given example, the type error is defined, and it prints the message on the console that it is the negative number error.

Let's see another example of throwing a Python exception. In the given example, if the list contains any non-integer value, then the program throws an exception.

#declaring a list
my_list=[1,2,3,7.7,'xyz']
#implementing a for loop
for i in my_list:
#checking the type of each list item
if not type(i) is int:
#throwing an exception if the element type is not an integer
raise Exception("The list contains non-integer value")
else:
print(i)

Output

Conclusion

An exception changes the normal flow of a program in regard to an error. In Python, we can throw an exception that is defined by users. For throwing the exception, we use Python's built-in raise keyword. This article explains the concept of throwing exceptions with examples.

How to Change Mouse and Touchpad Settings Using Xinput in Linux
Most Linux distributions ship with “libinput” library by default to handle input events on a system. It can process input events on both Wayland and X...
Remap your mouse buttons differently for different software with X-Mouse Button Control
Maybe you need a tool that could make your mouse's control change with every application that you use. If this is the case, you can try out an applica...
Microsoft Sculpt Touch Wireless Mouse Review
I recently read about the Microsoft Sculpt Touch wireless mouse and decided to buy it. After using it for a while, I decided to share my experience wi...