Detailed Notes
This episode of python certification walks through examples of exceptions. We look at try, exception, raise, and custom exceptions.
Useful Links: https://docs.python.org/3/tutorial/errors.html
Learn more about the overall certification and syllabus here: https://pythoninstitute.org/certification/pcap-certification-associate/pcap-exam-syllabus/
Github repository for code examples: https://github.com/robbroadhead/PythonCertificationSeries
Transcript Text
hello and welcome back we are continuing our season where we're looking at uh items for python certification this episode we are going to look at exceptions now this uh i got a little bit ahead of myself on that so i'll sort of walk through as i'm instead of coding as i often do so the way an exception works in python is you have a try and then you have an accept and try basically says i'm going to try this code and except says if i have a certain type of error or if i have an error depending on what it how you work it then i'm going to do something now the simple example here is we're going to go in we're going to do it we're going to ask the user to input a number and we're going to convert it to an int and then we're going to uh just print out hey you entered x so or you know whatever it is that you entered now what we're going to do here is uh and let me actually do it first without the try block so normally what would oh happen i can get here there we go so normally it's going to say oops sorry so let's do this so normally it's going to say enter number and you do and you enter it but if i enter uh a nod number then i'm going to get this value error invalid literal because i'm trying to convert i'm trying to do that end so what i can do is now since that is oh where'd that go see here that was a value error so now i'm going to say all right i'm going to do something with that in this case i'm going to do um actually that's not well we'll leave it just to try again because i'm going to fix that in a second but so right now then it's going to say hey instead of giving an hour of value error now so the first one if i enter a three it enters a three if i do an a it's gonna say oops that was not a number try again so now i have a clean and developer intentioned error as opposed to just it blows up now what we can do with this is if we could turn around and we could say so now we can actually with this if you remember a break because i'm in a while loop now and a break just says get out of that while loop and so now what i'm going to do is i'm going to say enter a number and either say you entered and be done with the loop or i'm going to ask you to do it again so now i have a much cleaner so if i do a 3 oh i'm sorry if i do a 3 then it's fine if i do an a oops try again okay let's try a g nope let's do this nope but if i do a number then boom so now i've got some logic and i've got a little more uh stability with my application now one of the things we might want to do in this one is because we're in this while loop what we could do is we could take the break out and have them enter a whole bunch of them but that means then what we're going to want to do is we're going to pick up the keyboard interrupt now if i do and i'll show you how i got that well actually got it because you can look stuff up but also so let's just run it like this um oh wait except for i'm not gonna let's do it without catching the interrupt and it's gonna catch value error but now it's just going to bail out because it doesn't tell me what that exception was i was thinking it would uh oh it doesn't because it's going to build up but now i can catch actually specifically the keyboard board interrupt which normally is going to get passed up to the operating system and break out now what i can do is let's see so if i do this i'm just checking this because this while true could be an infinite loop and could be a pane especially when you're dealing with keyboard interrupt so i want to make sure that if i get it and it works it's going to ask me again if i don't have a value or it asks me something else and if i get an interrupt then it's going to exit the loop okay let's clear this for a second so now if i enter a number see it's going to keep asking me to enter a number if i enter a letter it's going to say try again and if i hit ctrl c now it's going to fall into that control c exiting loop so now i've picked up the keyboard interrupt so you can actually stack errors you have multiple or exceptions you can have multiple exceptions that you handle specifically now another thing you can do is that you can have a straight out except so let me do let's do it this way let's just do accept uh and then we'll just do an error occurred whoops if i spell it right oh there we go okay so now what i can do is i don't have to if i don't know it i can just say all right i'm either going to probably doesn't like that oh so it does now like one or more handlers so what i'd have to do here is i can just say exception which is the base essentially or base exception well it doesn't like that three two one i'm sorry this actually that's a warning and not a problem so i can go back to this and now if i hit a number i'm fine oh because i'm in that while loop if i hit a letter it's is going to say an error occurred and now i have to break out of this thing because i killed it so pardon me a second so i did get into my infinite loop there as i said here i didn't have a break on it so it would just catch an error and it would just sit keep me in that infinite loop so let's do this now it goes in order uh let me bring that screen back up real quick first um it goes in order from top to bottom so that generic exception let's play with that a minute so if we go to that generic exception if i can type okay so if we do just a generic exception let's do this so if we just do accept here so this is going to take anything and then we're going to say oh actually now we're going to get an error because you're going to see that actually this one's going to say we're going to get errors because we will never actually hit these guys it starts with it goes in order and it says okay everything's gonna hit this because it is a generic however if i shift order then i say hey i've got these specific ones and if i don't catch if i've got any kind of other error then i'm going to break out or then i'm going to then i'm going to handle that exception so we'll just call it let's do uncaught exception now another thing we can do is let's do uh we'll keep it like that and then we'll just pick this one so now let's do this so now let's do a different try so we're just going to try print a new example and let's do uh ant 345. hey let's just do that and then that's going to blow up but this time what we're going to do i don't need the break is that we're actually going to take that exception whatever it is and we're going to use it and we can call it whatever we want but we're going to just say as this thing so let's just say exception we'll do it that way because it's a little more and you may use e sometimes people do that based on the other languages they've done so let's see what happens here when i try and play with that one oh that's right so let's see so i'd break out of that oh here we go and so uh the new example see it says invalid literal and that's what was pre um actually i probably should do this print um let's do it this way so it's a little easier to read and we're just going to do args and let's see how that works out okay uh i don't know whatever that was okay so here we go uh where is exception message in here oh but see this is another oh because i can't do that so i'm sorry because it's not going to oh because it's not you know i've got to convert this to a string my mistake so i got an exception and an exception which really blew things up okay let's see what that does so now when we break out we see so we come in a new example and we have this exception message that comes through because we've got it and so you can see here this exception message it says invalid literal so that's the message that i got with the base 10. so that's actually what happened if i look at the arguments then you can see here that the only arguments that i had for the exception is uh just the one which is basically just the message but now i can do with that actually let's do the same thing let's see let's do arg example and i can do a thing called raise and i want this to be um let's see one two three please space that just so it doesn't complain at me so now if i raise it's going to actually throw an exception and so now i can come in and oh here we go so then it's going to come in a new example arg example exceptions must derive from base exception so i can do uh let's do so i have to give it one and so i'm gonna do three two one so i want to give it actually in this case i have to actually tell it i'm sorry i can't just raise i have to tell it the exception type and so now if i do that break out so here my exception was this message so i could also do um let's try like let's do my exception name and oh let's see oh that's not a name that's my exception and it's gonna be let's see a name and a code and this is going to come from this exception three two one all right let's make this an exception so it does that and then what i can do is i can do raise my exception is going to be see negative 23. and it's going to just call it we'll call it henry and then three two one and let's get rid of that let's see this is going to be the name and the code and we're going to name and code and so if we look at this one so now we're raising a my exception and we can actually do we can leave it as exception we'll call we're gonna actually raise i actually do specifically on my exception and let's do this and we'll see here that the args now come across as that and we can actually do print name and do exception dot name actually we could do it like this we could do um so for that one we'll do it that way let's do it this way we'll just do this one so we can do name and we can do code and we can actually work with those individually ah break out of that and so we can see here that the name is henry and the code is negative 23. so we could actually do some pretty complicated exceptions if we need to and we can build on them we can we could log information we could do whatever we needed to but the key is for this one really is just to know that there's a try and accept you can have multiple accepts that there is a generic and essentially there's a catch-all which would you just accept this now we do have uh let's see well i think we'll go with that three two one but we do have one other thing and so we can do a uh where do i want to put it i'm gonna put it here we also have a finally which means regardless of what we do we're going to call the finale let me get rid of that so what we could do is we could say we're going to try this and no matter what we're going to break out of the loop something's going to happen and we're going to break out a loop so now if we do that we can see if we enter a number now boom it breaks out a loop if we enter a letter boom it breaks out a loop if we do ctrl c boom it breaks out a loop so this always gets called so we have our key pieces of exceptions as we have try we catch some exceptions we have one or more finally says hey we're going to do this no matter what whether it's a exception or not and then we can also raise exceptions so we can come in and do our own so we could do something like a like a number two low exception or you know value too high or something along those lines and or for example uh let's say a customer id we could have something we have a customer bad id exception that we raise that basically says if somebody says gives us a bad id we always do this and we always give them follow this logical path so that is exceptions in python and that'll wrap it up for this one so as always if you have any questions shoot an email to info development or dot com uh there will be up you know the repository will have examples of this in the exceptions.py file that's out there and you can see links in the show nut show notes but as always go out there have yourself a great day a great week and we will talk to you next time
Transcript Segments
hello and welcome back we are continuing
our season where we're looking at
uh items for python certification
this episode we are going to look at
exceptions
now this uh i got a little bit ahead of
myself on that so i'll sort of walk
through as i'm
instead of coding as i often do
so the way an exception works in
python is you have a try and then you
have
an accept and try basically says i'm
going to try this code
and except says if i have a certain type
of error
or if i have an error depending on what
it how you work it
then i'm going to do something now the
simple example here
is we're going to go in we're going to
do it we're going to ask the user to
input a number
and we're going to convert it to an int
and then
we're going to uh just print out hey you
entered
x so or you know whatever it is that you
entered
now what we're going to do here
is uh and let me actually do it first
without the try block
so normally what would
oh happen i can get here
there we go so normally it's going to
say
oops sorry
so let's do this
so normally it's going to say enter
number and you do and you enter it but
if i enter
uh a nod number then i'm going to get
this
value error invalid literal because i'm
trying to convert
i'm trying to do that end so what i can
do
is now since that is oh where'd that go
see here that was a value error so now
i'm going to say all right i'm going to
do something with that
in this case
i'm going to do um
actually that's not well we'll leave it
just to try again because i'm going to
fix that in a second but so right now
then it's going to say hey
instead of giving an hour of value error
now so the first one if i enter a three
it enters a three
if i do an a it's gonna say oops that
was not a number try again
so now i have a clean and
developer intentioned error
as opposed to just it blows up
now what we can do with this is if we
could turn around and we could say
so now we can actually with this if you
remember a break
because i'm in a while loop now and a
break just says get out of that while
loop
and so now what i'm going to do is i'm
going to say enter a number and either
say
you entered and be done with the loop or
i'm going to ask you to do it again so
now i have a much cleaner so if i do a 3
oh i'm sorry if i do a 3 then it's fine
if i do
an a oops try again okay let's try a g
nope let's do this nope but if i do a
number then boom
so now i've got some logic and i've got
a little more
uh stability with my application
now one of the things we might want to
do in this one is
because we're in this while loop what we
could do is we could take the break out
and have them enter a whole bunch of
them but that means then
what we're going to want to do
is we're going to pick up the keyboard
interrupt now if i do
and i'll show you how i got that well
actually got it because you can look
stuff up but also
so let's just run it like this um oh
wait except for i'm not gonna let's do
it without catching the interrupt
and it's gonna catch value error
but now it's just going to bail out
because it doesn't tell me what that
exception was i was thinking it would
uh oh it doesn't because it's going to
build up but now i can catch actually
specifically the keyboard board
interrupt which normally is going to get
passed up to the operating system and
break out
now what i can do is let's see so if i
do this
i'm just checking this because this
while true could be an infinite loop and
could be a pane
especially when you're dealing with
keyboard interrupt so i want to make
sure that if i
get it and it works it's going to ask me
again if i don't
have a value or it asks me something
else and if i get an interrupt then it's
going to exit the loop
okay let's clear this
for a second so now if i enter a number
see it's going to keep asking me to
enter
a number if i enter a letter it's going
to say try again
and if i hit ctrl c now it's going to
fall into that control c
exiting loop so now i've picked up the
keyboard interrupt
so you can actually stack errors you
have multiple or exceptions you can have
multiple exceptions that you handle
specifically
now another thing you can do is that you
can have
a straight out except
so let me do let's do it this way
let's just do accept
uh and then we'll just do
an error occurred whoops if i spell it
right
oh there we go
okay so now what i can do is i don't
have to if i don't know it
i can just say all right i'm either
going to
probably doesn't like that
oh so it does now
like one or more handlers
so what i'd have to do here is i can
just say
exception which is the base essentially
or base exception
well it doesn't like that
three two one i'm sorry this actually
that's a warning and not a problem so i
can go back
to this
and now if i hit a number i'm fine oh
because i'm in that while loop
if i hit a letter it's is going to say
an error occurred
and now i have to break out of this
thing because
i killed it so pardon me a second
so i did get into my infinite loop there
as i said here i didn't have a break on
it
so it would just catch an error and it
would just sit keep me in that infinite
loop
so let's do this
now it goes in order uh let me bring
that
screen back up real quick first um
it goes in order from top to bottom so
that generic
exception
let's play with that a minute so if we
go to that generic
exception
if i can type
okay
so if we do just a generic exception
let's do this so if we just do accept
here
so this is going to take anything and
then we're going to say
oh actually now we're going to get an
error because you're going to see that
actually
this one's going to say we're going to
get errors because we will never
actually hit
these guys it starts with it goes in
order and it says okay everything's
gonna hit this
because it is a generic however if i
shift
order then i say hey i've got these
specific ones and if i don't catch if
i've got any kind of other error
then i'm going to break out or then i'm
going to
then i'm going to handle that exception
so we'll just call it let's do uncaught
exception now another thing we can do
is let's do
uh we'll keep it like that and then
we'll just pick this one so
now let's do this so now let's do a
different try
so we're just going to try print
a new example
and let's do uh ant
345. hey let's just do that
and then that's going to blow up but
this time what we're going to do
i don't need the break is that we're
actually going to
take that exception whatever it is and
we're going to use it and we can call it
whatever we want but we're going to just
say as
this thing so let's just say
exception we'll do it that way because
it's a little more
and you may use e sometimes people do
that based on the other languages
they've done
so let's see what happens here
when i try and play with that one
oh that's right
so let's see so i'd break
out of that oh here we go
and so uh the new example see it says
invalid literal
and that's what was pre um actually i
probably should do this print um
let's do it this way so it's a little
easier to read
and we're just going to do args
and let's see how that works out okay
uh i don't know whatever that was okay
so
here we go uh where is exception message
in here
oh but see this is another oh because i
can't do that
so i'm sorry because it's not going to
oh because it's not you know i've got to
convert this to a string my mistake
so i got an exception and an exception
which really blew things up
okay let's see what that does so now
when we break out
we see so we come in a new example and
we have this exception message
that comes through because we've got it
and so you can see here this exception
message it says invalid literal
so that's the message that i got with
the base 10. so that's actually what
happened
if i look at the arguments
then you can see here that the only
arguments that i had for the exception
is uh just the one which is basically
just
the message but now i can do
with that actually let's do the same
thing
let's see let's do arg example
and i can do a thing called raise
and i want this to be um
let's see one two three
please space that just so it doesn't
complain at me
so now if i raise it's going to actually
throw an exception
and so now i can come in
and
oh here we go so then it's going to come
in
a new example arg example exceptions
must derive from base exception so
i can do
uh
let's do so i have to give it one and so
i'm gonna do
three two one so i want to give it
actually
in this case i have to actually tell it
i'm sorry i can't just raise i have to
tell it the exception type
and so now if i do that break out
so here my exception was this message
so i could also do um let's try like
let's do my exception
name
and oh let's see
oh that's not a name that's
my exception and it's gonna be let's see
a name and a code
and this is going to come from
this exception
three two one all right let's make this
an
exception so it does that and then what
i can do is i can do raise
my exception
is going to be
see negative 23. and
it's going to just call it we'll call it
henry
and then
three two one and let's get rid of that
let's see
this is going to be the name
and the code
and we're going to name and code
and so if we look at this one
so now we're raising a my exception
and we can actually do
we can leave it as exception we'll call
we're gonna actually raise i actually do
specifically on my exception
and let's do this
and we'll see here that the args now
come across as
that and we can actually do
name and do exception dot
name
actually we could do it like this we
could do
um so for that one
we'll do it that way let's do it this
way we'll just do this one so we can do
name and we can do
code
and we can actually work with those
individually
ah break out of that and so we can see
here that the name is henry and the code
is negative 23. so we could actually do
some pretty complicated exceptions
if we need to and we can build on them
we can we could log information we could
do
whatever we needed to but the key is
for this one really is just to know that
there's a try and accept
you can have multiple accepts that there
is
a generic and essentially there's a
catch-all which would you
just accept this now we do have
uh let's see
well i think we'll go with that
three two one but we do have one other
thing
and so we can do a
uh where do i want to put it i'm gonna
put it here
we also have a finally
which means regardless of what we do
we're going to call the finale
let me get rid of that so what we could
do is we could say we're going to try
this and no matter what we're going to
break out of the loop
something's going to happen and we're
going to break out a loop so now if we
do that
we can see if we enter a number now boom
it breaks out a loop
if we enter a letter boom it breaks out
a loop if we do ctrl c
boom it breaks out a loop so this always
gets called
so we have our key pieces of exceptions
as we have try we catch some exceptions
we have one or more
finally says hey we're going to do this
no matter what whether it's a
exception or not and then we can also
raise
exceptions so we can come in and do our
own so we could do something like a
like a number two low exception or you
know value too high or something along
those lines
and or for example uh let's say a
customer id we could have something we
have a customer bad id
exception that we raise that basically
says if somebody says
gives us a bad id we always do this and
we always
give them follow this logical path
so that is exceptions in python and
that'll wrap it up for this one so as
always if you have any questions
shoot an email to info development or
dot com uh there will be up
you know the repository will have
examples of this in the
exceptions.py file that's out there and
you can see
links in the show nut show notes but
as always go out there have yourself a
great day a great week
and we will talk to you next time