Raise As Exception
Raise As Exception
The raise statement in Python is used to raise an exception. Try-except blocks can be used
to manage exceptions, which are errors that happen while a programme is running. When
an exception is triggered, the programme goes to the closest exception handler, interrupting
the regular flow of execution.
o The raise keyword is typically used inside a function or method, and is used to
indicate an error condition.
o We can throw an exception and immediately halt the running of your programme
by using the raise keyword.
o Python looks for the closest exception handler, which is often defined using a try-
except block, when an exception is triggered.
o If an exception handler is discovered, its code is performed, and the try-except
block's starting point is reached again.
o If an exception handler cannot be located, the software crashes and an error message
appears.
Example:
# divide function
if b == 0:
return a / b
try:
result = divide(10, 0)
except Exception as e:
In this illustration, the division function accepts the inputs a and b, and if b equals zero,
an exception is raised. The try block catches this exception, and the unless block prints
the error message.
Example-2
try:
check_age(17)
except Exception as e:
print(" An error occurred: ", e)
Output:
The check age method in this illustration accepts the input age and throws an exception if
age is less than 18. The try block catches this exception, and the unless block prints the
error message. This demonstrates how you can raise and handle exceptions in Python.
You can raise any type of exception, such as ValueError, TypeError, KeyError, etc. to
indicate specific errors in your code.
output:
Invalid Value
We attempt to change the string "a" to an integer in this instance, which results in a
ValueError. The first unless block recognises the exception and produces a message
stating that the value is incorrect. A different kind of exception would be handled by the
next except block if it were to arise.
The finally block can also be employed to run code that has to be run whether or not this
exception was triggered. For instance:
Code
try:
# code that might raise an exception
result = 10 / 0
except ZeroDivisionError:
# code that handles the exception
print(" Cannot divide by zero. ")
finally:
# code that must be executed regardless of whether an exception was raised or not
print(" The finally block is always executed. ")
Output:
try:
input_num = int(input("Enter a number: "))
if input_num < number:
raise InvalidAgeException
else:
print("Eligible to Vote")
except InvalidAgeException:
print("Exception occurred: Invalid Age")
For example, consider a voting system. It takes various details of the person as well as
checks if all the values entered are appropriate. Here, we are trying to create a class that
performs this task as well as throws an exception in case the age of the person is less than
18.
class VotingPerson(Exception):
# Raised when the input age is less than 18
def __inti__(self, name, id, age):
self.name = name
self.id = id
self.age = age
try:
name = input("Enter name")
id = int(input("Enter id"))
age = int(input("Enter age"))
if age<18 :
raise VotingPerson
vote1 = VotingPerson(name,id,age)
print("Thanks for Voting")
except Exception as e:
print("Sorry you are not eligible for voting",e)