Advertisement



< Prev
Next >



Python - Try-Except Block





In order to handle an exception thrown in a program, we can use the try-except blocks. The try-except blocks are made up of two individual blocks - a try block and an except block.





The except block(preceded by an except keyword) 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 thrown by try block, the exception is caught within this except block and the program continues its normal execution without ending abruptly.









How to catch an exception using try-except blocks?


Exception thrown 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 thrown by the 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 blocks")

Output :


An exception is caught
Outside try-except blocks


As you can see, an exception of type ZeroDivisionError is thrown 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 thrown 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 an "as" keyword


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

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 an as keyword
	print("An exception caught : ",arg) # A message related to caught exception is printed
	
print("Outside try-except blocks") 		

Output :


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






Mismatch in an exception thrown and an 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 thrown 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 meth1() method in try block
except ZeroDivisionError as e:
	print("An exception is caught - ", e)


Output:


Caught exception : int type

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

PYTHON and DJANGO


Advertisement