HackerRank Exceptions problem solution in Python YASH PAL, 31 July 202417 January 2026 HackerRank Exceptions Python Solution – In this HackerRank Exceptions problem solution in Python, Errors detected during execution are called exceptions.Examples: ZeroDivisionErrorThis error is raised when the second argument of a division or modulo operation is zero.>>> a = '1' >>> b = '0' >>> print int(a) / int(b) >>> ZeroDivisionError: integer division or modulo by zero ValueErrorThis error is raised when a built-in operation or function receives an argument that has the right type but an inappropriate value.>>> a = '1'>>> b = '#'>>> print int(a) / int(b)>>> ValueError: invalid literal for int() with base 10: '#'Handling ExceptionsThe statements try and except can be used to handle selected exceptions. A try statement may have more than one except clause to specify handlers for different exceptions. #Code try: print 1/0 except ZeroDivisionError as e: print "Error Code:",e OutputError Code: integer division or modulo by zeroTaskYou are given two values a and b.Perform integer division and print a/b.HackerRank Exceptions solution in Python 2.for t in xrange(int(input())): try: a,b = map(int,raw_input().split()) print a/b except ZeroDivisionError as e: print "Error Code: %s" % e except ValueError as e: print "Error Code: %s" % e Exceptions solution in Python 3.# Enter your code here. Read input from STDIN. Print output to STDOUT for i in range(int(input())): try: a,b=map(int,input().split()) print(a//b) except Exception as e: print("Error Code:",e)Problem solution in pypy programming.# Enter your code here. Read input from STDIN. Print output to STDOUT #from __future__ import division for i in xrange(input()): try: a,b = map(int, raw_input().split()) print a/b except Exception as e: if isinstance(e, ZeroDivisionError): stringy = str(e).split() stringy.insert(2, "or") stringy.insert(3, "modulo") print 'Error Code:'," ".join(map(str, stringy)) else: print 'Error Code:',e Problem solution in pypy3 programming.# Enter your code here. Read input from STDIN. Print output to STDOUT # Enter your code here. Read input from STDIN. Print output to STDOUT from sys import stdin,stdout n = stdin.readline() for i in range(0,int(n)): line = stdin.readline().strip('nr') a,b = line.split(" ") try: print(str(int(a)//int(b))) except ZeroDivisionError as e: print("Error Code: {}".format(e)) except ValueError as e: print("Error Code: {}".format(e)) coding problems solutions Hackerrank Problems Solutions Python Solutions HackerRankPython