Exception Handling in Python (Part II)
Raising an Exception
Raise keyword to raise built-in exceptions
We can handle runtime exceptions in Python but we can also raise it deliberately using raise keyword. In Exception handling, raise clause is used to raise an exception. We can also pass a message or a value to an exception indicating the reason why that exception was raised.
Syntax :
raise [Exception_name[,args[,traceback]]]
Here,
Exception_name is a name of exception to be raised
args is a value given to the exception.This ia an optional parameter.If it is not given then it is considered to be None
traceback it is an optional parameter.It is rarely used in practice but if it is present is used to trace and display the reason of occurrence of exception .Even if exception occurred at certain statement but reason for that may be somewhere in other parts of code to denote that this argument can be used
Example of raising exception using raise keyword without passing argument
""" Author : ITVoyagers (https://itvoyagers.in/) Date :28th December 2018 Description : Program to check the age entered is in valid range """ age=int(input("enter a your age")) try: if age<0 or age>100: raise Exception() #here no parameter is passed print("valid age ",age) except Exception as e: print("age out of range ",e) #no value for e is given out as no parameter is passed in raise statement
OUTPUT
>>> enter a your age20 valid age 20 >>> ================================ RESTART ================================ >>> enter a your age200 age out of range >>>
Example of raising exception using raise keyword by passing argument
""" Author : ITVoyagers (https://itvoyagers.in/) Date :28th December 2018 Description : Program to check the age entered is in valid range """ age=int(input("enter a your age")) try: if age<0 or age>100: raise Exception(age) #here parameter is passed print("valid age ",age) except Exception as e: print("age out of range ",e) #value for e is given out as parameter is passed in raise statement
OUTPUT
>>> enter a your age20 valid age 20 >>> ================================ RESTART ================================ >>> enter a your age200 age out of range 200 >>>
Note : Both examples shows difference in outputs based on argument being passed and other without argument
User-Defined Exceptions
Raise keyword to raise User-Defined Exceptions
Sometimes there is a need certain customized exceptions.To create this customized exceptions user can write their own code for exception handling.
Syntax :
class classname(Exception): pass try: statement(s) raise classname except classname: print("statements of except clause") else: print("statements if no exception raised")
Here, classname can be any name. This classname is used in raise and except key word to raise an exception
Note : To define customized exception class it is mandatory to derive that class from base class Exception
Example of User-Defined Exceptions
""" Author : ITVoyagers (https://itvoyagers.in/) Date :28th December 2018 Description : Program to create user defined exception class """ #to create user defined exception class we need to use Exception base class as parameter #class_name can be any name to be derived from Exception base class class MyErrorClass(Exception): pass val=int(input("enter the number")) try: if val!=100: raise MyErrorClass #user defined Exception classname except MyErrorClass: print("Exception raised value no matched") else: print("value matched")
OUTPUT
>>> enter the number18 Exception raised value no matched >>> ================================ RESTART ================================ >>> enter the number5 Exception raised value no matched >>>