Exception Handeling
Exception Handeling
Exceptions
Exceptions:
● Exceptions are raised when the program is syntactically correct, but the code
resulted in an error.
● Exceptions does not stop the execution of the program, however, it changes
the normal flow of the program.
● An exception can be defined as an unusual condition in a program resulting in
the interruption in the flow of the program.
Common Exceptions
1. ZeroDivisionError: Occurs when a number is divided by zero.
2. NameError: It occurs when a name is not found. It may be local or global.
3. IndentationError: If incorrect indentation is given.
4. IOError: It occurs when Input Output operation fails.
5. EOFError: It occurs when the end of the file is reached, and yet operations
are being performed.
The try-except statement
● If the Python program
contains suspicious code that
may throw the exception, we
must place that code in the
try block.
● The try block must be
followed with the except
statement, which contains a
block of code that will be
executed if there is some
exception in the try block.
Example
Try -except-else
● We can also use the else
statement with the try-except
statement.
● Else block executed if no
exception occurs in the try
block.
1. try:
2. a = int(input("Enter a:"))
3. b = int(input("Enter b:"))
4. c = a/b
5. print("a/b = %d"%c)
6.
7. # Using Exception with except statement. If we
print(Exception) it will return exception class
8.
9. except Exception:
10. print("can't divide by zero")
11. print(Exception)
12. else:
13. print("Hi I am else block")
Exception
● Python provides the flexibility not to specify the name of exception with the
exception statement. E.g
● try:
● a = int(input("Enter a:"))
● b = int(input("Enter b:"))
● c = a/b;
● print("a/b = %d"%c)
● except:
● print("can't divide by zero")
● else:
● print("Hi I am else block")
Except statement using with exception variable
● We can use the exception
variable with the except
statement.
● It is used by using the as
keyword.
● this object will return the
cause of the exception.
Points to remember
● Python facilitates us to not specify the exception with the except statement.
● We can declare multiple exceptions in the except statement since the try
block may contain the statements which throw the different type of
exceptions.
● We can also specify an else block along with the try-except statement, which
will be executed if no exception is raised in the try block.
● The statements that don't throw the exception should be placed inside the
else block.
Try- Except with Else
Declaring Multiple Exceptions
Declaring multiple
exceptions is useful in
the cases where a try
block throws multiple
exceptions.
The try...finally block
● Python provides the optional
finally statement, which is
used with the try statement.
● It is executed no matter
what exception occurs and
used to release the external
resource.
● The finally block provides a
guarantee of the execution.
Raising exceptions
● An exception can be raised forcefully by using the raise clause in Python.
● It is useful in in that scenario where we need to raise an exception to stop the
execution of the program.
For example, there is a program that requires 2GB memory for execution,
and if the program tries to occupy more than 2GB of memory, then we can
raise an exception to stop the execution of the program.
Raise Exceptions
● To raise an exception, the raise
statement is used. The exception class
name follows it.
● An exception can be provided with a
value that can be given in the
parenthesis.
● To access the value "as" keyword is
used. "e" is used as a reference
variable which stores the value of the
exception.
● We can pass the value to an exception
to specify the exception type.
Raise Exceptions
Raising Exception
Custom Exception
● The Python allows us to
create our exceptions that
can be raised from the
program and caught using
the except clause.
● Custom class should inherit
BaseException class or
subclass of BaseException
End