Python Fucntions
Python Fucntions
Python Fucntions
lower = 900
upper = 1000
# The pass is also useful in places where your code will eventually go, but has not been written
yet : for example) −
for letter in 'Python':
if letter == 'h':
pass
print 'This is pass block'
print 'Current Letter :', letter
Exception Handling in is one of the effective means to handle the runtime errors so that the
regular flow of the application can be preserved. Python Exception Handling is a mechanism to handle
runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc.
Exception is an unwanted or unexpected event, which occurs during the execution of a program,
i.e. at run time, that disrupts the normal flow of the program’s instructions. Exceptions can be caught
and handled by the program. When an exception occurs within a method, it creates an object. This
object is called the exception object. It contains information about the exception, such as the name
and description of the exception and the state of the program when the exception occurred.
Advantage of Exception Handling: The core advantage of exception handling is to maintain
the normal flow of the application. An exception normally disrupts the normal flow of the
application; that is why we need to handle exceptions.
To understand the exception firstly we should know about the error in program. Errors in program can
be categorized into two types.
Compile Time Errors
Run Time Errors
Syntax Errors
Logical Errors
Compile Time Errors:- Errors caught during compiled time is called Compile time errors. Compile
time errors include library reference, syntax error or incorrect class import.
Run Time Errors:- The error that occurs during the run time of program is called run time error.
Syntax Error: Errors are the problems in a program due to which the program will stop the
execution
Logical Errors: :- The error that occurs during the logics which are interpret in the program.
They are also known as exceptions. Hence we can say that exception is a runtime error that occurs
because of user's mistake.
Reasons of Exception
Mismatched Input :Suppose that we are entering our name in place of age,causing exception
because age is of data type int and name will be string.
File does not exist :Suppose that we are reading a text file easy.txt and that file does not
exist in the system, causing exception.
Divide by zero exception :When a number is divided by zero then the output will be
undefined(infinity).
IndentationError: If incorrect indentation is given.
Addition of two incompatible types
Trying to access a nonexistent index of a sequence
Removing the table from the disconnected database server.
ATM withdrawal of more than the available amount
Error vs. Exceptions
Error Exceptions
All errors in Python are the unchecked type. Exceptions include both checked and unchecked
type.
Errors occur at run time which unknown to the Exceptions can be recover by handling them with
compiler. the help of try-catch blocks.
Errors are mostly caused by the environment in The application itself causes exceptions.
which an application is running.
Examples: Examples:
OutofMemoryError Checked Exceptions, SQL exception,
NullPointerException,etc.
try:
#block of statements
except:
#block of statements
else:
#block of statements
finally:
#block of statements
#Example:
# initialize the amount variable
marks = 67
# perform division with 0
a = marks / 0
print(a)
#Example Complete
try:
a = int(input(" Please Enter any no for division: "))
b = int(input(" Please Enter any divisor: "))
c=a/b
except ValueError as v:
print("sum does not exist and ", v)
except ZeroDivisionError as e:
print("Cannot divide by 0 and ",e)
except:
print("Something went wrong")
else:
print("else block")
print("the op/ is = " ,c)
finally:
print("Mandatory Block ")
print(" I AM ALWAYS WITH YOU!!!!!!")