Python Tutorial 15 --- “Try Except Exception Handling In Python”

 Python Tutorial 15 ---

 “Try Except Exception Handling In Python”

This PROGRAM or TUTORIAL is about “Try Except Exception Handling In Python”

DOC

Before discussing exceptional handling, let us discuss, what an exception is actually.

“Exception can be said as an error, that causes a program to crash. Unlike syntax error, it is syntactically correct and occurs mostly due to our negligence”

For example, assigning a string value to an int data type variable or dividing a number by zero or also when a name of a variable or a function is not found, an exception occurs. Python has built-in support for dealing with many sorts of exceptions automatically, but we can also define our own.

   The solution to exception related problems is simple. We just have to alter the normal flow of the program by a bit of code known as an exception handler. This code will save the state of the program up to the point where the exception occurred and will continue the normal flow from the code written outside the area of occurrence of an exception. We can also print the exception by converting it into a string. This way program does not terminate but executes completely except for the area where the exception occurred.

Now that we have covered the basics, let us move towards an in-depth understanding of exception handling. Try and except blocks are used in Python to handle the exception. If you are familiar with any other programming language, the except block is the same as the catch block. In the try block, we write the code about which we have doubt that exception could occur in it, and in except block we just write the code that we want to execute in case the exception error occurs. In such cases where no exception occurs, the except block will not execute. In simple words, in the try block, we write the code where chances of exception are high, and in except block, we handle the error, maybe through printing a warning or just skipping the exception part completely by completely ignoring it as a part of the program.

We can also use an else keyword to print something if no exception occurs. For example. In case of an exception, the except block is printing the error, likewise, if the exception does not occur, we could print a statement that no error occurred, using an else keyword.

There are also many sorts of predefines exceptions that we could find in Python such as EOF or End of File Error (occurs when the end of a file is reached but the operation is not completed) or ZeroDivisionError (occurs when the number is divided by zero). We can code such expect blocks that catch only specific sort of exception and ignore the rest. For this purpose, we have to specify the error name after the keyword except, before putting the colon.

Advantages of using try and catch:

  • Without a try block if an exception occurs the program will surely crash.
  • If we have some part of code that is not much important but can cause an exception, then we must write it down in the try block so it does not cause the whole program to crash.

CODE

#Try Except Exception Handling

print("Enter Number 1")
num1 = input()
print("Enter Number 2")
num2 = input()
try:
    print("Sum of Num1+Num2 is:", int(num1)+int(num2))

except Exception as e:
    print(e)


print("Important demo line")

Comments

Popular posts from this blog

Python Tutorial 38 --- "Self & __init__() (Constructors)(OOPS 3)"

Python Exercise 4 --- "Number Guessing Game"

Python Tutorial 13 --- "Short Hand If Else Notation In Python"