4.1 try, except, and finally

I think a good way to introduce these keywords is with one of your exercises - determining whether a solution is acidic or basic given its pH.

I’m sure you know that if a solution’s pH is less than 7, that it is acidic (and vice versa); similarly, if a solution’s pH is exactly 7, then we say that the solution is neutral.

However, let’s take this a step further. You probably know the following equation for calculating a solution’s pH:

\[\begin{equation*} \text{pH} = -\log([\text{H}^+]) \end{equation*}\]

Where:

  1. \(\text{pH}\) is the pH of the solution
  2. \([\text{H}^+]\) is the concentration of H+ ions in the solution.

Suffice to say that \([\text{H}^+]\) must be greater than 0 (i.e., you cannot take the logarithm of zero or a negative number). Hence, in Python, I can do something like this:

from math import log

hConc = float(input("Enter your solution's H+ concentration here: "))

print(-log(hConc) if hConc > 0 else "You didn't enter a valid H+ concentration!")

And this works (barely)! However, what if the user were to enter something like 2x as their H+ concentration? Your program would crash as is - Python would return a value error (i.e., Python’s way of saying that it cannot carry out a conversion).

Luckily, there is another way of dealing with the issue using Python’s try and except:

from math import log

try:
  hConc = float(input("Enter your solution's H+ concentration here: "))
  print(-log(hConc))
except:
  print("You didn't enter a valid H+ concentration")

You can copy my code into Colab, Spyder, or into the IDE of your choice and enter 2x as your input to see how these keywords work!

4.1.1 How can I go about thinking of try and except?

try and except’s functions can both be summarized in the below code block:

# The rest of your code here...

try:
  '''
  Your code here.  If any part of our code here returns an error...
  '''
except:
  '''
  ...then run the code inside this block!  If the code inside here also produces an error, then your program WILL crash!
  '''

Because of this, try and except are great error-handling methods that you may wish to consider implementing in your final projects for this course.

4.1.2 And finally…

There’s try and except, but there’s also the finally keyword. Going back to the example on calculating the pH of a solution, I can use the finally keyword like such:

from math import log

try:
  hConc = float(input("Enter your solution's H+ concentration here: "))
  print(-log(hConc))
except:
  print("You didn't enter a valid H+ concentration")
finally:
  print("All done here!")

Do make the necessary changes in your IDE or in Colab. Run the code - what did you notice?

The statement "All done here!" was printed out to your console - code indented beneath the finally keyword will always run no matter what!

# The rest of your code here...

try:
  '''
  Your code here.  If any part of our code here returns an error...
  '''
except:
  '''
  ...then run the code inside this block!  If the code inside here also produces an error, then your program WILL crash!
  '''
finally:
  '''
  But whatever the case, ALWAYS run the code in here!
  '''

And this - in essence - is what finally does! However, note that finally is not mandatory if you ever find yourself using a try and an except block (i.e., if you do not have a need for finally, then you can always omit it from your code).