Advertisement
raise type-of-exception(argument, traceback)
Exception Class | Description |
---|---|
type-of-exception | This is type of an exception to be raised explicitly. It could be defined with either the name of exception class or with an object of exception class, that you are raising. |
argument | This gives us a string message associated with the exception which we are going to raise explicitly. This is an optional attribute and if it is not defined then its value is None i.e. no value. |
traceback | This is an object which gives us the stack trace from the point of an exception handler, down the call stack, to the point where the exception was raised. This is also an optional attribute. |
#Python - Using raise keyword to raise an exception from try block by using an object of exception class
import sys
# Variables a and b are initialized with 10
a,b = 10,10
try:
numerator = a+b
denominator = a-b
if denominator==0:
#Creating an object of type ZeroDivisionError exception
ob=ZeroDivisionError('A number could be divided by zero')
#raising an exception of type ZeroDivisionError through its object
raise ob
else:
c = numerator/denominator
except ZeroDivisionError as arg: # A matching exception of type ZeroDivisionEror is declared in except block
print("An exception caught : ",arg) # A message related to caught exception is printed
print(sys.exc_info()) # This function gives the name of caught exception which was thrown by try block
print("Outside try-catch block")
An exception caught : A number could be divided by zero
(<class 'ZeroDivisionError'>, ZeroDivisionError('A number could be divided by zero'), <traceback object at 0x0000000002EF8248>)
Outside try-catch block
Advertisement
#Python - Using raise keyword to raise an exception from try block by using the name of exception class
import sys
a,b = 10,10 # Variables a and b are initialized with 10
try:
numerator = a+b
denominator = a-b
if denominator==0:
raise ZeroDivisionError('A number could be divided by zero')
else:
c = numerator/denominator
except ZeroDivisionError as arg: # A matching exception of type ZeroDivisionEror is declared in except block
print("An exception caught : ",arg) # A message related to caught exception is printed
print(sys.exc_info()) # This function gives the name of caught exception which was thrown by try block
print("Outside try-catch block")
An exception caught : A number could be divided by zero
(<class 'ZeroDivisionError'>, ZeroDivisionError('A number could be divided by zero'), <traceback object at 0x0000000002EF83C8>)
Outside try-catch block
# Python - Using raise keyword to raise an exception from except block.
import sys
try:
i = 'one'
ab = i + 2
except TypeError as e:
print("Exception caught - ",e)
raise TypeError
Exception caught - can only concatenate str (not "int") to str
Traceback (most recent call last):
File "D:/Python Programs/exp_raise.py", line 7, in <module>
ab = i + 2
TypeError: can only concatenate str (not "int") to str
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/Python Programs/exp_raise.py", line 10, in <module>
raise TypeError
TypeError
# Python - Using the raise keyword to raise a different exception from what is already caught by except block
import sys
try:
i
except NameError as e:
print("Exception caught - ",e)
ob = BufferError('Raising an exception of type BufferError')
raise ob
Exception caught - name 'i' is not defined
Traceback (most recent call last):
File "D:/Python Programs/exp_raise3.py", line 7, in <module>
i
NameError: name 'i' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/Python Programs/exp_raise3.py", line 11, in <module>
raise ob
BufferError: Raising an exception of type BufferErrorr
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement