This program is just to understand the working and use of functions and try-except in Python
A function is a block of code which only runs when it is called.You can pass data, known as parameters, into a function.A function can return data as a result.
Function can be called anytime by using the function name followed by parenthesis. EXAMPLE: my_function()
Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. EXAMPLE: def my_function(name,age):
If 2 arguments are given the function also expects exactly 2 arguments, else error will be shown.
#Try
The try block lets you test a block of code for errors.
#Except
The except block lets you handle the error.
#Finally (Using of finally is not shown in the above code)
The finally block lets you execute code, regardless of the result of the try- and except blocks.
#Exception Handling
When an error occurs ,Python will normally stop and generate an error message.
These EXCEPTIONS can be handled using the TRY STATEMENT. Since the try block raises an error, the except block will be executed (in except block we gives what to be shown when error occurs like print somthing or to do something). Without the try block, the program will crash and raise an error.
#Many Exceptions
You can define as many exception blocks as you want,e.g. if you want to execute a special block of code for a special kind of error. This can be done by making a new except block blow the previos except block.
#Else You can use the else keyword after exception to define a block of code to be executed if no errors were raised.