Exception Handling Exercises

Exception Handling

  1. List the Python keywords used in exception handling and what they do:
  2. Research 5 or more exceptions and what they do.
  3. except-statements
    1. What is the maximum number of except-statements a Python script can have?
    2. What is the maximum number of except-statements that will execute?
    3. What is the minimum number of except-statements a Python script can have?
    4. What is the minimum number of except-statements that will execute?
  4. finally-statements
    1. What is the maximum number of finally-statements a Python script can have?
    2. What is the maximum number of finally-statements that will execute?
    3. What is the minimum number of finally-statements a Python script can have?
    4. What is the minimum number of finally-statements that will execute?
  5. By default, when a user pressing the control (CTRL) and "c" keys on the keyboard, a Python script will stop executing. Write a Python script that displays all of the numbers between 1 and 1,000,000. If the user presses CTRL-c, ask the user if they would like to continue. If they enter "y", have the script keep counting where it left off.

    Example:
    1
    2
    3
    ...
    ^C397046
    397047
    397048
    Would you like to keep counting (Y/N)? y
    397048
    397049
    ^C397050
    397051
    397052	    
    Would you like to keep counting (Y/N)? n
    Have a nice day!

Error Checking and Exception Handling

  1. Discuss with someone else the major differences between error handling and exception handling.
  2. Should you always use error checking (if-statements) instead of exception handling?
  3. Give an example of when to use error checking instead of exception handling.