Docsity

Handling Exceptions, Debugging & Loops in CSSE 120 at Rose-Hulman, Study notes of Software Engineering

Partial preview of the text

Download Handling Exceptions, Debugging & Loops in CSSE 120 at Rose-Hulman and more Study notes Software Engineering in PDF only on Docsity!

EXCEPTION HANDLING,

DEBUGGING, AND

INDEFINITE LOOPS

CSSE 120—Rose Hulman Institute of Technology

Conditional Program Execution

 Programs (scripts)  Modules designed to run directly (not imported)  Libraries  Modules imported and designed not to run directly  Hybrid  Can do both  Add to end of module:  if name == 'main' main() Two underscores Great for testing!

The Need for Exception Handling

 Separates main code from code for special cases

 Keeps the main code clean  Makes the expected special cases clear  Says ―do these steps and if an exception occurs, handle it this way‖

 Example:

 Download from Lessons  Modules to Download …  Session 10  slope.py  Try with: (5, 6) and (10, 4) (3, 6) and (10, 12) (3, 300) and (3, 500)

Exception Handling: try statement

 Can use if statement to take care of this special case.

 We can instead use exception handling: try statement try:

except :

Algorithm code goes in body of try clause

Exceptions are caught and handled in except clause.

Type of exception that could be generated

Multiple ―except‖ Clauses

 Like multi-way decisions with if-elif-else statements try:

except :

… except :

except:

Multiple ―except‖ Clauses Example

 Try (3, 300) and (a, 500)

 Modify slope.py to include an except clause for a syntax error exception

 Try (2, 100) and (4 100) missing comma!

 Try

 **>>> dir(builtins)

shows names of exceptions**

Using a Debugger

 Typical debugger commands:

 Set a breakpoint—place where the debugger will pause the program  Single step—execute one line at a time  Inspect a variable—look at its changing value over time

 Debugging Example

 Download printFactorial.py from Modules to Download  Session 10

Sample Debugging Session: Eclipse

This is the Debug perspective

A all the variables view that shows

Ashows all the view that executingfunctions

This shows the line being view is an editor that executed and lets you makechanges to the file

Athe outline of the view that shows module beingexamined ( Outline View )

Review: Definite Loops

 Review: For loop

 Definite loop: knows a priori the number of iterations of loop body  Counted loop: sequence can be generated by range()  Example for loop in slideshow.py

 Syntax:

 for in :

Is This Loop a Definite Loop?

#Open the file inputFile = open(inputFileName, 'r')

process each line of file

for line in inputFile: image = Image(imageCenter, line.rstrip()) image.draw(win) time.sleep(delay)

win.getMouse() inputFile.close() win.close()

While Loop

 A pre-test loop

 Condition is tested at the top of the loop

 Example use of while loops

Nadia deposits $100 in a savings account each month. Each month the account earns 0.25% interest on the previous balance. How many months will it take her to accumulate $10,000?

Combining While Loops and

Exception Handling

 Download getFile.py from Modules to Download  Session 10  Put file in same project that we've been using  Run the module with various inputs, like:  getFile.py  slope.py  notARealFileName

 Let's fix it!

Hint: You'll need code like the fixed version of this for your homework!

Speed Reading

 See Homework 10 instructions on Angel: Lessons  Homework  Homework 10

 Reading and ANGEL quiz due session 11

 Problem 3:

 Due session 12, but you'll have more homework assigned session 11 so start now!  Pair Programming