Exception Handling
Exception handling:
Raw code before exception handling:
def sum(num1, num2):
print(num1+num2)
number1 = input("Enter a number: ")
sum(number1, 12)Traceback (most recent call last):
File "c:\Users\$USERNAME\Desktop\test.py", line 6, in <module>
sum(number1, 12)
File "c:\Users\$USERNAME\Desktop\test.py", line 2, in sum
print(num1+num2)
TypeError: can only concatenate str (not "int") to strCode after exception handling:
Exception handling should be used only if you don't have any control over how it is used
The above code does not need any exception handling really, as the data type is in our control
Last updated