Day #38 - Raising Custom Error

Day #38 - Raising Custom Error

Β·

3 min read

Introduction

Welcome to my 38th blog post. In blog number 36, we learned about exception handling in python using try...except block. In our code sometimes we want to manually raise an error to stop the program and avoid unexpected circumstances or events that could cause harm to the product or the service or the project that you are working on. So in today's blog number 38, we will learn to raise our own custom error using the 'raise' keyword. Now Let's dive into more details.

So let's get started......

Raising Custom errors

In python, we can raise custom errors by using the raise keyword.

Example 1 -

salary = int(input("Enter salary amount: "))
if not 2000 < salary < 5000:
    raise ValueError("Not a valid salary")

Example 2 -

a = int(input("Enter any value between 5 and 9"))

if(a<5  or a>9):
  raise  ValueError("Value should be between 5 and 9")
Enter any value between 5 and 910
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    raise  ValueError("Value should be between 5 and 9")
ValueError: Value should be between 5 and 9

In the above example, ValueError is raised manually as the entered value outside the range 5 and 9.

Defining Custom Exceptions

In the previous tutorial, we learned about different built-in exceptions in Python and why it is important to handle exceptions. However, sometimes we may need to create custom exceptions that serve our purpose.

In Python, we can define custom exceptions by creating a new class that is derived from the built-in Exception class.

Here's the syntax to define custom exceptions:

Syntax

class CustomError(Exception):
  # code ...
  pass

try:
  # code ...

except CustomError:
  # code...

This is useful because sometimes we might want to do something when a particular exception is raised.

Resources Used

You can watch the video of Day#38 by clicking on the below link πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡

Conclusion

Thanks, guys for going through this blog post. In this blog post, we learned about how we can raise a custom error manually to stop the program and avoid unexpected circumstances. To raise a custom error, we make use of 'raise' keyword.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding. This is it for Day #38

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


Β