Python Class On Debugging
Python Class On Debugging
1) Arithmetic Error
2) Type Error
3) Index Error
and so on so forth......
>>> ls=[1,2,3]
>>> ls[4]
ls[4]
>>> a=4
>>> b=0
>>> a/b
a/b
>>> a=4
>>> A
>>> a='4'
>>> b=5
>>> a+b
a+b
>>> ls=[1,2,3]
>>> ls+'iglobal'
ls+'iglobal'
>>> ls='iglo'bal'
>>> ls=[1,2,3]
>>> ls.group()
ls.group()
--> Any Error encountered during execution of the program is called RUNTIME
error
Exception Handling
-->if there is Exception, handle them using Try-Except block to execute the
program NORMALLY.
for i in range(4):
try:
c=a/i
print(c)
except ZeroDivisionError:
ls=[1,2,3]
l=len(ls)
for i in range(l+1):
try:
k=ls[i]/2
print(k)
except IndexError:
print('since index value is not existing, we consider the last list element')
k=ls[i-1]/2
print(k)
def add(n,a):
try:
c=n+a
print(c)
except TypeError:
g=str(n)
a='iglobal'
add(n,a)
#sample program to multiple try-except
a=int(input('enter a '))
b=[1,2,0,4]
while True:
for i in range(len(b)+1):#0,1,2,3,4
try:
c=e/b[i]
print(c)
except NameError:
e=int(input('enter the value of e '))
break
except ZeroDivisionError:
except IndexError:
if i==len(b):
break
else:
continue
#sample program on multiple try and except
def f1(a,b):
for i in a:
try:
c=i/b
print(c)
except TypeError:
try:
c=int(i)/b
print(c)
except ZeroDivisionError:
b=1
c=int(i)/b
print(c)
print(a)
print(b)
f1(a,b)
#sample program for one TRY and multiple except
A finally clause is always executed before leaving the try statement, whether an
exception has been handled or not. When an exception has occurred in the try
clause and has not been handled by an except clause (or it has occurred in an
except or else clause), it is re-raised after the finally clause has been executed.
a=3
b=0
try:
c=a/b
except IndexError:
finally:
Raising Exceptions
def square(l,b):
if(l==0):
elif(b==0):
raise Exception('breadth cannot be zero ')
else:
return l*b
try:
k=square(l,b)
def area(l,b):
if(l==0):
elif(b==0):
else:
area=l*b
while True:
try:
area(l,b)
print('bye')
break
print('valueerror:',err)
continue
Debugging
Tools and techniques for finding bugs and help in fixing errors faster with less
effort.
The program will stay paused until press one of the five buttons in the Debug
Control window: Go, Step, Over, Out, or Quit.
1) Go
Clicking the Go button will cause the program to execute normally until it
terminates or reaches a breakpoint.
If debugging is done and want the program to continue normally, click the Go
button.
2) STEP
Clicking the Step button will cause the debugger to execute the next line of code
and then pause again.
The Debug Control window’s list of global and local variables will be updated if
their values change.
If the next line of code is a function call, the debugger will “step into” that
function and jump to the first line of code of that function.
Clicking the Over button will execute the next line of code, similar to the Step
button. However, if the next line of code is a function call, the Over button will
“step over” the code in the function.
The function’s code will be executed and the debugger will pause as soon as the
function call returns.
For example, if the next line of code is a print() call, don’t really care about code
inside the built-in print() function; just want the string you pass it printed to the
screen. For this reason, using the Over button is more common than the Step
button.
If you have stepped into a function call with the Step button and now simply want
to keep executing instructions until you get back out, click the Out button to “step
out” of the current function call.
To stop debugging entirely and not to continue executing the rest of the
program, click the Quit button.
The Quit button will immediately terminate the program. If you want to run your
program normally again, select Debug▸Debugger again to disable the debugger.
#sample program
def f1(a,st):
print(' I am in f1 ')
a = 48
st = 'raju'
b = 45.4
r = a + 12
return r
a = 24
st = 'python'
print(a,st)
rt = f1(a,st)
print(a,st)
Breakpoints
A breakpoint can be set on a specific line of code and forces the debugger to
pause whenever the program execution reaches that line.