Try/Except - The Boredless Tourist - Just can't figure it out

I started out in the Python3 course and changed lanes into the Introduction to Computer Science course, which dovetails with Python3.
Early on in the Python3 course, there was some mention of Try/Except. A “here’s the basic concept, but we’ll flesh it out later” type situation. But it wasn’t revisited. There also hasn’t been any mention of it in the Computer Science course (as far as I recall) until this point, where I’m expected to apply it.
I’ve Googled documentation: It’s inaccessible to me, I can’t wrap my head around it. It’s written in this opaque jargon only programmers understand. I’ve searched outside of official documentation and everyone explaining it is doing so with concepts that are beyond the scope of what I’ve learned on CodeCademy. I’ve watched three or four YouTube videos and have been met with the same problem. I just cannot find someone who is explaining Try/Except in a way that is accessible to a [CodeCademy] beginner. It’s really driving me crazy. I don’t understand it at all. I haven’t come here to the CodeCademy forums, because I’m afraid it’ll be the same thing. I don’t know how to articulate what it is I’m not getting.
I was expecting the course to hold my hand and walk me through it like I’m five, but it skipped right from “There’s the lake, we’ll teach you how to dog paddle next week,” to “In you go, son, swim like I taught you.”
Can anybody point me to a learning resource, ideally one geared toward CodeCademy beginners, that can illuminate Try/Except for me?

Hello! Essentially, try…except are just like if…else, except, instead of checking for equality if "hi" == 1, etc, they check for errors in the code. For example:

try:
  print("hi"
except:
  print("That was wrong")

In that snippet, the try checkss the code within it (print("hi"). If that code has an error, then instead of throwing an error, the program simply does not run that code, and runs whatever is in the except block. In this case, since print("hi" will throw a Syntax error (missing parenthesis), instead of giving this error:

SyntaxError: unexpected EOF while parsing

the except block will run, and so

"That was wrong"

will be printed.
Here is some reading on it.

I hope this helps!

2 Likes

When I write that example code…

try:
  print("Hi"
except:
  print("Problem")

…what I get is a SyntaxError on line 3 with the little arrow pointing to the e in except:. It doesn’t print “Problem” like it’s supposed to do. What am I doing wrong? I only wrote those four lines, exactly as you see them above this paragraph.

image

The worst part is I can’t even Google why this is happening because any combination of an error code and “python try except” just yields tutorials on try-except blocks.

Try this:

try:
  prnt("hi")
except:
  print("That was wrong")

There would usually be an error for prnt, as it isn’t defined and isn’t a keyword. Sorry my first example was flawed!

3 Likes

Ahh, OK this turned out to be something important to learn. It turns out the open parentheses in print("Hi" were what was causing the problem. Not every problem can be interpreted as an error.
Thank you! Oh my god, the relief I feel finally being able to move past this is huge.

1 Like