Exception Handling 1
Exception Handling 1
Exception Handling 1
In the above example, the statements that can cause the error are placed
inside the try statement (second print statement in our case). The second
print statement tries to access the fourth element of the list which is not
there and this throws an exception. This exception is then caught by the
except statement.
Catching Specific Exception
• A try statement can have more than one except clause, to specify
handlers for different exceptions. Please note that at most one
handler will be executed. For example, we can add IndexError in
the above code. The general syntax for adding specific exceptions
are –
try:
# statement(s)
except IndexError:
# statement(s)
except ValueError:
# statement(s)
Example: Catching specific exception in Python
# Program to handle multiple errors with one
# except statement
# Python 3
def fun(a):
if a < 4:
# throws ZeroDivisionError for a = 3
b = a/(a-3)
# throws NameError if a >= 4
print("Value of b = ", b)
try:
fun(3)
fun(5)
# note that braces () are necessary here for
# multiple exceptions
except ZeroDivisionError:
print("ZeroDivisionError Occurred and Handled")
except NameError:
print("NameError Occurred and Handled")
• Try with Else Clause
• In python, you can also use the else clause on the try-except
block which must be present after all the except clauses. The
code enters the else block only if the try clause does not raise an
exception.
• Example: Try with else clause
# Program to depict else clause with try-except
# Python 3
# Function which returns a/b
def AbyB(a , b):
try:
c = ((a+b) / (a-b))
except ZeroDivisionError:
print ("a/b result in 0")
else:
print (c)
finally:
# this block is always executed
# regardless of exception generation.
print('This is always executed')
Raising Exception
The raise statement allows the programmer to force a specific
exception to occur. The sole argument in raise indicates the
exception to be raised. This must be either an exception instance
or an exception class (a class that derives from Exception).
try:
raise NameError("Hi there") # Raise Error
except NameError:
print ("An exception")
raise # To determine whether the exception was raised or
not
The output of the above code will simply line printed as “An
exception” but a Runtime error will also occur in the last due
to the raise statement in the last line. So, the output on your
command line will look like
Syntax:
try:
# Some Code
except:
# Executed if error in the
# try block
How try() works?
First, the try clause is executed i.e. the code between try and
except clause.
If there is no exception, then only the try clause will run, except
the clause is finished.
If any exception occurs, the try clause will be skipped and except
clause will run.
If any exception occurs, but the except clause within the code
doesn’t handle it, it is passed on to the outer try statements. If the
exception is left unhandled, then the execution stops.
A try statement can have more than one except clause
Code 1: No exception, so the try clause will run.
# Python code to illustrate
# working of try()
def divide(x, y):
try:
# Floor Division : Gives only Fractional Part as
Answer
result = x // y
print("Yeah ! Your answer is :", result)
except ZeroDivisionError:
print("Sorry ! You are dividing by zero ")
In python, you can also use the else clause on the try-except
block which must be present after all the except clauses. The
code enters the else block only if the try clause does not raise an
exception.
Syntax:
try:
# Some Code
except:
# Executed if error in the
# try block
else:
# execute if no exception
# Program to depict else clause with try-except
finally:
# this block is always executed
# regardless of exception generation.
print('This is always executed')
Some of the common built-in exceptions are other
than above mention exceptions are:
Exception Description
# Constructor or Initializer
def __init__(self, value):
self.value = value
try:
raise(MyError(3*2))
Some other Python Libraries available for creating our own GUI
applications are
Kivy
Python Qt
wxPython
What are Widgets
Widgets in Tkinter are the elements of GUI application which
provides various controls (such as Labels, Buttons,
ComboBoxes, CheckBoxes, MenuBars, RadioButtons and many
more) to users to interact with the application.
Canvas It is used to draw pictures and others layouts like texts, graphics etc.
Message It works same as that of label and refers to multi-line and non-editable text
It is used to provide a graphical slider which allows to select any value from that
Scale
scale
Text It allows user to edit multiline text and format the way it has to be displayed
Method Description