0% found this document useful (0 votes)
58 views

Python Class On Debugging

The document discusses different types of errors in Python like arithmetic, type, index errors and provides examples. It then discusses exception handling using try-except blocks to handle errors and continue execution. Finally it discusses debugging tools like breakpoints in Python debugger to pause execution at specific lines and inspect variables.

Uploaded by

Krishnaprasad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Python Class On Debugging

The document discusses different types of errors in Python like arithmetic, type, index errors and provides examples. It then discusses exception handling using try-except blocks to handle errors and continue execution. Finally it discusses debugging tools like breakpoints in Python debugger to pause execution at specific lines and inspect variables.

Uploaded by

Krishnaprasad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Types of Errors:

1) Arithmetic Error

2) Type Error

3) Index Error

and so on so forth......

>>> ls=[1,2,3]

>>> ls[4]

Traceback (most recent call last):

File "<pyshell#11>", line 1, in <module>

ls[4]

IndexError: list index out of range

>>> a=4

>>> b=0

>>> a/b

Traceback (most recent call last):

File "<pyshell#14>", line 1, in <module>

a/b

ZeroDivisionError: division by zero

>>> a=4

>>> A

Traceback (most recent call last):


File "<pyshell#16>", line 1, in <module>

NameError: name 'A' is not defined

>>> a='4'

>>> b=5

>>> a+b

Traceback (most recent call last):

File "<pyshell#19>", line 1, in <module>

a+b

TypeError: Can't convert 'int' object to str implicitly

>>> ls=[1,2,3]

>>> ls+'iglobal'

Traceback (most recent call last):

File "<pyshell#21>", line 1, in <module>

ls+'iglobal'

TypeError: can only concatenate list (not "str") to list

>>> ls='iglo'bal'

SyntaxError: invalid syntax

>>> a=int(input('enter the value of a '))

enter the value of a 34.5

Traceback (most recent call last):

File "<pyshell#23>", line 1, in <module>


a=int(input('enter the value of a '))

ValueError: invalid literal for int() with base 10: '34.5'

>>> ls=[1,2,3]

>>> ls.group()

Traceback (most recent call last):

File "<pyshell#1>", line 1, in <module>

ls.group()

AttributeError: 'list' object has no attribute 'group'

--> Any Error encountered during execution of the program is called RUNTIME
error

-->IF a Runtime error is encountered, program terminates abruptly. Normally this


situation is called an EXCEPTION.

Exception Handling

-->if there is Exception, handle them using Try-Except block to execute the
program NORMALLY.

#sample program to use Try-except Block

a=int(input('enter the value of n '))

for i in range(4):

try:

c=a/i

print(c)

except ZeroDivisionError:

print("i value cannot be zero")


#sample program to provide a solution using Try-Except block for Index error

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)

#sample program using user defined function for handling exceptions

def add(n,a):

try:

c=n+a

print(c)

except TypeError:

print('since integer and string cannot be added')

g=str(n)

print('possible answer could be',a+g)

n=int(input('enter the value of 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:

print('b value cannot be zero ')

except IndexError:

print('no value exists')

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:

print('since b cannot be zero reassigning it as 1 ')

b=1

c=int(i)/b

print(c)

a=list(input('enter the value of a '))

print(a)

b=int(input('enter the value of b '))

print(b)

f1(a,b)
#sample program for one TRY and multiple except

Use of Finally Statement:

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.

#sample program on finally clause

a=3

b=0

try:

c=a/b
except IndexError:

print('the value of b cannot be zero')

finally:

print('program terminates here')

Raising Exceptions

#sample program to raise our exceptions

def square(l,b):

if(l==0):

raise Exception('length cannot be zero ')

elif(b==0):
raise Exception('breadth cannot be zero ')

else:

return l*b

for l,b in ((2,5),(5,4),(4,0)):

try:

k=square(l,b)

print('the area value is ',k)

except Exception as err:

print('the error ......', err)

#sample program to raise our exceptions

def area(l,b):
if(l==0):

raise Exception('length cannot be zero')

elif(b==0):

raise Exception('breadth cannot be zero ')

else:

area=l*b

print('area of rectangle is',area)

l=int(input('enter the length value '))

b=int(input('enter the breadth value '))

while True:

try:

area(l,b)

print('bye')

break

except Exception as err:

print('valueerror:',err)

b=int(input('enter the correct breadth value '))

continue
Debugging

Tools and techniques for finding bugs and help in fixing errors faster with less
effort.

The debugger is a feature of IDLE that executes a program one instruction at a


time, giving chance to inspect the values in variables while code runs, and track
how the values change.

It is helpful to see the actual values in a program while it runs

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.

3) Over (Don’t enter into inbuilt or user defined 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.

4) Out ( Out of the function)


Clicking the Out button will cause the debugger to execute lines of code at full
speed until it returns from the current function.

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.

Quit ( Quit from the program)

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.

Testing for Local and Global Variables

Control is in F1() local variables :: a: 48, st :raju

Global: a =24, st :python

#sample program

def f1(a,st):

print(' I am in f1 ')

a = 48

st = 'raju'

b = 45.4

print(' End of the f1 ')

r = a + 12
return r

a = 24

st = 'python'

print(a,st)

rt = f1(a,st)

print(' Return value = ', rt)

print(a,st)

Looking into Print functions:

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.

Without Breakpoint, Click GO, completes execution

Set Breakpoint stops execution

You might also like