Raising Custom Errors in Python. Topic – 26

Raising Custom Errors in Python

In Python, we can raise customs errors. If you are coding in Python and you want to throw an error on a specific action, how would you do that? To do this you have to use the raise keyword. We all know that errors occur due to mistakes in our code. But sometimes we would like to raise errors ourselves. Now you will think why do we create problems for ourselves? One of the biggest reasons for raising the error is that we would sometimes want to stop the program, and not have any unexpected errors. Let me explain.

Just assume, we say the user to enter a number between 20 to 30. If the user enters outside the range of 20 to 30 we can raise a custom error.

num = int(input("Enter a number between 20 to 30: "))
if(num<20 or num>30):
    raise ValueError("number should be between 20 to 30")
    print("Perfect enter")

The Output Of This Code Will Be:

Enter a number between 20 to 30: 35
Traceback (most recent call last):
  File "d:\Online Class\Python Class\tempCodeRunnerFile.py", line 3, in <module>
    raise ValueError("number should be between 20 to 30")
ValueError: number should be between 20 to 30

Sometimes error is also good. Because it is possible that the error is on line number 5, and because of line number 5 there is an error on line number 15. So if the error occurs on line number 5 then why should we take the program to line number 15!. We would always like the program to stop where an error occurs. A good programmer writes programs with this approach.

For example, your girlfriend has asked you to bring dairy milk from the shop. The shopkeeper has mistakenly given you KitKat instead of dairy milk. So will you take KitKat and then your girlfriend will tell you that this is KitKat, I want dairy milk. You will go again to the shop to change the chocolate. Of course not. You will say at the first time to the shopkeeper. Custom errors also work similarly. Stops it where there is an error. Does not allow the programmer to proceed further.

Some errors in Python are automatically handled. As you are asked to input an integer, the user is given a string. Python will handle this error. But how will Python know that you need a value between 20 and 30? For this, you need to raise custom errors.

In Python programs, we can create custom exceptions by creating a new class. Syntex look like this:

class CustomError(Exception):
  # code ...

  # code ...

except CustomError:
  # code...

If a programmer encounters a syntax error, we handle it using try and except. And if a user is trying to perform a different task by providing incorrect input, we give custom errors. These errors are mainly for making the application secure.

If you don’t know the Exception Handling In Python” just click on Read More.

I hope you understand “Raising Custom Errors in Python” Need guidance or have questions? Drop a comment below. Share your email for a personalized touch—I’ll send exclusive resources and answer queries. Let’s code together, creating efficient code and memorable learning moments! 🚀

Finite number of  lines code and infinite possibilities

Programmer, Arpan Bera

Leave a Reply

Your email address will not be published. Required fields are marked *

Arpan Bera

Coding is like Love – everyone can start, but only a few finish without errors

Keep Updated to our News and Blog