Advertisement



< Prev
Next >



Python - Try-Except Block





In order to handle an exception raised in a Python program, we can use a try-except block, which is made up of two individual blocks - a try block and an except block.





Preceded by the except keyword, an except block declares a type of exception that it is going to catch and handle. If the type of exception declared in an except block is same as the type of exception raised by its associated try block, the exception is caught within this except block and the program continues its normal execution without ending abruptly.

Note: Sometimes, the except block may also be referred to as except statement.









How to catch an exception using try-except blocks?


Exception raised has a certain type. In order to catch an exception of a specific type, we have to make sure that our except block has declared the same type of exception that is raised by its try block.


Let's understand this by an example -:
# Python - try-except block

a,b= 10,0
try:
	c=a/b   			# If a is divided by b(which has a value 0)
	print("The result of a/b is : ", c)
		
except ZeroDivisionError:        	# A matching type of exception is declared in except block
	print("An exception is caught") # A message related to caught exception is printed in except block
	
print("Outside try-except block")

Output :


An exception is caught
Outside try-except block


As you can see in the program, an exception of type ZeroDivisionError is raised when a statement enclosed within the try block is executed. This exception is caught by the except block because it is declared with a matching exception of type ZeroDivisionError and a message of our choice "An exception is caught" is printed.

As the raised exception of type ZeroDivisionError is caught by the except block, hence, the program does not end abruptly and it continues to successfully execute the statement outside the try-except blocks, which prints a message - "Outside try-catch block".


Advertisement




Except block with the "as" keyword


When declaring an except statement, we can optionally use the as keyword, which helps us declare an argument, using which we could print a useful message associated with the caught exception.

Let's understand this by an example -:
# Python - try-except blocks example

a,b= 10,0
try:
	c=a/b   			  # If a is divided by b(which has a value 0)
	print("The result of a/b is : ", c)
		
except ZeroDivisionError as arg:           # An except block is declared with the as keyword
	print("An exception caught : ",arg) # A message related to caught exception is printed
	
print("Outside try-except block") 		

Output :


An exception caught :  division by zero
Outside try-except block






Mismatch between exception raised and exception declared in except block





Let us understand this by an example here.
class TryCatch2:
	def meth(self):
		try:
			print(100/0); 	#ZeroDivisionError exception is raised here
			print("Hello from meth function")
		except IOError :	#Non-matching exception-type is declared with this except statement		
			print("We have caught an exception")
		
		print("Outside try-except blocks")

ob = TryCatch2()
try:
	ob.meth() 			#enclosing the call to meth() method in try block
except ZeroDivisionError as e:
	print("An exception is caught - ", e)


Output:


An exception is caught -  division by zero

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement