Python raises exception

In this article, we are going to learn about how can we raise exceptions in Python. The concept of exception handling is very important and useful in programming, and in Python as well, we can do exception handling. First of all, we would understand what is an exception, and then we would understand how can we raise some exceptions, and how is it useful for us.

Python raises exception

What is an Exception?

Well, an exception can be defined as some event, which disturbs the normal flow of our program. For example, let’s say that you are trying to open some file to read data from it in the program, and the file is not at the specified location, so this is an exception FileNotFoundError. If otherwise, the file was there, it would have been able to do the operations, but since the file was not found, it did not know what to do, and it just terminated abnormally.

So, in python, we can handle the exception. For that, we just write the critical instructions within the try block, and we have the corresponding except for blocks for exceptions if they occur.

What is the need to raise exceptions?

In our Python programs, there are situations, when some exception is thrown and we need to deal with it. But at times, we might need to raise some exceptions ourselves. For example, let’s say that you are writing a program, and in that, you are just accepting a number greater than or equal to zero as user input, and if the user enters some number that is less than zero, then you want to raise an exception. In such cases, we would want to raise the exception ourselves, and here comes the raise keyword to our rescue.

Also, if we are creating our custom exceptions, then also, we would need to raise them whenever and wherever required. So, in such situations as well, we can make use of the raise keyword to raise the exception.

How to raise exceptions?

Now, that we have a brief idea about exceptions, and the need for raising exceptions ourselves, let’s have a look at a simple python program, through which, we can understand the use of the raise keyword, and also understand how can we raise exceptions in our python programs.

Program example to raise an exception –

number = int(input(“Please enter a number greater than or equal to 0: “))
if number < 0:
    raise Exception(“Sorry… no negative numbers allowed here. “)
else:
    print(f”You entered the number: {number}”)

As you can see in the above program, we are just taking some number as user input, and then we are just checking if the number is less than zero, and if the condition is True, then we are raising an Exception, with some message. Otherwise, we are just doing normal things.

So, the thing is that whenever you want to raise some exception by yourselves, then you can simply use the raise keyword, and raise the required exception.

Note that we are raising an exception here, and the exception type should be the subclass of BaseException.

Let’s consider another example, just for more clarity. You might be familiar with the ATM machine. Let’s say that we have an ATM machine, from which, we can withdraw the amount, which is multiple of 500. so, if the user enters some amount that violates this rule, then we might not want to continue the transaction and exit. Let’s have a look at a simple and dummy demonstration for the same –

amount = int(input(“Enter The amount: (only multiples of 500 are allowed): “))
if amount%500 == 0:
    print(“You can proceed with the withdrawal!”)
else:
    raise ValueError(“Sorry… you entered an invalid amount!”)

As you can see in the above simple program, we are just getting the amount as user input, and then we are just checking if the amount is a multiple of 500. If the condition is True, then we can just proceed with the further transaction, and if not, we are going to raise an exception with some message.

You can try to execute the above program and observe the output. The thing is that the above programs will terminate abnormally when the exception is raised since we are not handling the exception, but raising it.

Conclusion:

In this article, we have understood how can we raise some exceptions in our python programs. We have made use of the raise keyword in Python, to raise some exceptions. 

We can understand that we can make use of the raise keyword, to raise some exceptions in our Python program. Also, with the above programs, you can understand the syntax for using the raise keyword, to raise some exceptions.

FAQ About Python raise an exception

Q: What is an exception?

Ans: Exception can be considered as some event, which occurs during the execution of the program, which disrupts the normal flow of the program execution.

Q: How can we raise some exceptions in python?

Ans: We can make use of the raise keyword, to raise some exceptions in Python.

Q: What is exception handling in Python?

Ans: At times, in our python program, we might face some exceptions in our programs when executing, resulting in abnormal termination of the program. The thing is that we can handle the exceptions. Python gives us the power to handle the exception. We just need to write the critical instructions within the try block, and for the exceptions that might occur in the program, we write the except blocks.