When We're Up In A Spaceship, That's When I Feel The Greatest: Python Errors

When We're Up In A Spaceship, That's When I Feel The Greatest: Python Errors

[Wrote this weeks ago wanting to finish. As I am moving to a more structured way of doing Python just wanted to get that one out there please - thanks.]

Try/Excepts

I finally understand these after a year of trying.

All it is is this: the code will run inside of the try block. If the code inside of the try block fails; the program then moves to the except block and then executes the code inside of that block. Then the program will continue running. 

I love these.

Catching Specific Exceptions

If you put an "except" inside of a "try" clause, you can handle just about any error. However, most often times, we know what kind of an error we are going to encounter or we expect to encounter. Therefore, it is considered best practice to be as specific as possible with the types of errors we want to raise - thank you. Unless - sometimes - we might want to be as open as possible with the sorts of exceptions we want to catch.

You just do this by writing "except" and then writing the name of the error that you want. Please note that this only handles the specific type of exception - thanks.

I learned to rase an exception "as" something.

Handling Multiple Exceptions

We can list more than one Exception type in a tuple.

This is where we can see the whole benefit of raising an Exception "as" something - by catching the error AS something, we can print and see it as a message, and even operate on it, EVEN when the error is one of a list of errors in a Tuple - as we don't yet know which of the errors it will be until it is caught - THANK YOU.

You can also just have a list of excepts.

This allows you to handle each of the errors individually and more specifically.

From the course:

"Note that the order of handlers is important here - if an exception is encountered, Python will execute the first one that matches its type."

So this means that the first exception that gets encountered will be executed in the code and therefore the order is very important here too.

A green image with bright pink, black and orange text. Green background. Text reads: historic python update before I move on with my course tomorrow



Comments

Popular posts from this blog

Hello World

Yosemite

Two Lines of Thought, Makes Me Wonder What I'm Missing