# Learning Python 3 with the Linkbot/Dealing with the imperfect
### closing files with with

We use the "with" statement to open and close files.
If some sort of error happens anywhere in this code (one of the files is inaccessible, the parse function chokes on corrupt data, etc.) the "with" statements guarantee that all the files will eventually be properly closed. Closing a file just means that the file is "cleaned up" and "released" by our program so that it can be used in another program.

### catching errors with try

So you now have the perfect program, it runs flawlessly, except for one detail, it will crash on invalid user input. Have no fear, for Python has a special control structure for you. It's called try and it tries to do something. Here is an example of a program with a problem:
Notice how when you enter @#& it outputs something like:
As you can see the int function is unhappy with the number @#& (as well it should be). The last line shows what the problem is; Python found a ValueError. How can our program deal with this? What we do is first: put the place where errors may occur in a try block, and second: tell Python how we want ValueErrors handled. The following program does this:
Now when we run the new program and give it @#& it tells us "That was not a number." and continues with what it was doing before.
When your program keeps having some error that you know how to handle, put code in a try block, and put the way to handle the error in the except block.

### Generating Errors: Controlling a Linkbot's Speed

We've seen in previous examples that we can write a function that makes a wheeled robot travel a certain distance. We can also control the rotational velocity of the motors with the setJointSpeed function. The setJointSpeed function expects a rotational speed with units of degrees/sec, but it would be nice if we could have a function where we could set the robot speed using inches/sec. The math equation to convert $v$ inches/sec to $\omega$ degrees/sec is
$\omega \mathrm {(deg/sec)} ={\frac {v\mathrm {(inches/sec)} }{r\mathrm {(inches)} }}*{\frac {180\mathrm {(degrees)} }{\pi \mathrm {(radians)} }}$
where $r$ is the wheel radius. Lets expand our example from the Learning Python 3 with the Linkbot/Defining Functions section:
This example is all well and good. We define a new function setSpeed that sets the speed of a Linkbot wheeled vehicle and we use it to set the speed to 2.5 inches per second.
What if the programmer tries to set the speed to 1,000 inches/second? Or 1,000,000 inches/second? Although it would be cool to see a Linkbot compete with a Formula One race car, there are physical limitations that prevent the Linkbot's motors from moving more than 200 degrees/second. If the speed is too high, we should set an error the user can see and possibly deal with. This is called "raising an exception". The code to raise an exception looks like this:
When an exception is raised, the function immediately returns with the exception. These raised exceptions can be caught by try/except blocks. If the exception occurred outside of a try/except block, the entire program will quit and display the error message of the exception. In the setSpeed function, this means that if the raise is executed, the two setJointSpeed statements will be skipped.
When I run the new program and I try to set the speed to 1000 inches a second, I get this output:
Now you can use try/catch blocks to deal with possible errors. Lets try writing a program that tries to set the speed to 10 inches per second again, except every time it encounters an exception, in reduces the requested speed by 1 inch/second and tries again.
The output is
Lets step through this program together to make sure we fully understand what is happening.
* # 1 : When we first get to this line, we create a new variable called requestedSpeed and set its value to "10".
* # 2 : Enter an infinite loop
* # 3 : Try to set the speed. requestedSpeed is currently 10, which is too high. The setSpeed function raises an exception. Since we are in a try/except block, we immediately go to the except block since an exception was thrown. Proceed to # 5
* # 5 : Decrease requestedSpeed by one. requestedSpeed is now 9. This is the end of our while loop, which means that Python goes back to the beginning of the loop.
* # 3 : We end up at # 3 again, except requestedSpeed is now 9. Still too high, exception is thrown.
* # 5 : Again, we decrease requestedSpeed to 8.
* # 3 : Still too high...
* # 5 : Reduce to 7...
* # 3 : Still too high...
* # 5 : Reduce to 6.
* # 3 : Now it succeeds. Since it succeeded, no exception was raised. Continue to # 4
* # 4 : This break statement pops us out of the loop. Proceed to # 6 and the rest of the program.

### Exercises

Update at least the phone numbers program (in section Dictionaries) so it doesn't crash if a user doesn't enter any data at the menu.
* ↑ "The 'with' statement"
* ↑ 'The Python "with" Statement by Example'