📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Exceptions

2021-07-29 •Youtube

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
0

hello and welcome back we are continuing

2.32

our season where we're looking at

4

uh items for python certification

7.52

this episode we are going to look at

9.44

exceptions

11.92

now this uh i got a little bit ahead of

13.84

myself on that so i'll sort of walk

15.44

through as i'm

16.16

instead of coding as i often do

19.76

so the way an exception works in

22.88

python is you have a try and then you

25.84

have

26.4

an accept and try basically says i'm

29.92

going to try this code

31.679

and except says if i have a certain type

35.2

of error

35.84

or if i have an error depending on what

37.76

it how you work it

39.04

then i'm going to do something now the

41.28

simple example here

43.6

is we're going to go in we're going to

44.48

do it we're going to ask the user to

46

input a number

47.12

and we're going to convert it to an int

49.76

and then

50.64

we're going to uh just print out hey you

54.239

entered

54.719

x so or you know whatever it is that you

57.44

entered

58.239

now what we're going to do here

61.28

is uh and let me actually do it first

63.68

without the try block

67.68

so normally what would

70.96

oh happen i can get here

74.159

there we go so normally it's going to

76.32

say

77.36

oops sorry

80.56

so let's do this

85.759

so normally it's going to say enter

86.799

number and you do and you enter it but

88.24

if i enter

89.759

uh a nod number then i'm going to get

92.72

this

93.84

value error invalid literal because i'm

96.96

trying to convert

98.159

i'm trying to do that end so what i can

101.28

do

105.119

is now since that is oh where'd that go

108.88

see here that was a value error so now

110.32

i'm going to say all right i'm going to

111.2

do something with that

112.799

in this case

115.92

i'm going to do um

119.68

actually that's not well we'll leave it

121.04

just to try again because i'm going to

122.079

fix that in a second but so right now

123.68

then it's going to say hey

125.6

instead of giving an hour of value error

129.039

now so the first one if i enter a three

131.84

it enters a three

132.72

if i do an a it's gonna say oops that

135.44

was not a number try again

138.319

so now i have a clean and

141.68

developer intentioned error

145.04

as opposed to just it blows up

148.319

now what we can do with this is if we

149.76

could turn around and we could say

153.92

so now we can actually with this if you

157.04

remember a break

158.56

because i'm in a while loop now and a

160.08

break just says get out of that while

161.519

loop

163.28

and so now what i'm going to do is i'm

164.56

going to say enter a number and either

166.48

say

166.8

you entered and be done with the loop or

170

i'm going to ask you to do it again so

173.04

now i have a much cleaner so if i do a 3

176.56

oh i'm sorry if i do a 3 then it's fine

179.36

if i do

179.92

an a oops try again okay let's try a g

183.76

nope let's do this nope but if i do a

186.48

number then boom

188.72

so now i've got some logic and i've got

190.959

a little more

192.56

uh stability with my application

196.159

now one of the things we might want to

197.36

do in this one is

199.599

because we're in this while loop what we

201.76

could do is we could take the break out

205.44

and have them enter a whole bunch of

206.879

them but that means then

209.04

what we're going to want to do

212.799

is we're going to pick up the keyboard

214.319

interrupt now if i do

218.72

and i'll show you how i got that well

220.239

actually got it because you can look

221.519

stuff up but also

223.04

so let's just run it like this um oh

226.159

wait except for i'm not gonna let's do

227.76

it without catching the interrupt

233.519

and it's gonna catch value error

240.56

but now it's just going to bail out

245.04

because it doesn't tell me what that

246.959

exception was i was thinking it would

248.64

uh oh it doesn't because it's going to

250.159

build up but now i can catch actually

251.76

specifically the keyboard board

253.519

interrupt which normally is going to get

254.72

passed up to the operating system and

256.4

break out

258.16

now what i can do is let's see so if i

260.479

do this

261.44

i'm just checking this because this

262.56

while true could be an infinite loop and

264.16

could be a pane

265.36

especially when you're dealing with

266.4

keyboard interrupt so i want to make

268.08

sure that if i

268.96

get it and it works it's going to ask me

270.8

again if i don't

273.6

have a value or it asks me something

274.88

else and if i get an interrupt then it's

277.12

going to exit the loop

278.479

okay let's clear this

282.24

for a second so now if i enter a number

285.199

see it's going to keep asking me to

286.8

enter

287.12

a number if i enter a letter it's going

288.96

to say try again

292.16

and if i hit ctrl c now it's going to

295.6

fall into that control c

296.96

exiting loop so now i've picked up the

298.8

keyboard interrupt

300

so you can actually stack errors you

302.639

have multiple or exceptions you can have

304.24

multiple exceptions that you handle

305.6

specifically

307.52

now another thing you can do is that you

310.479

can have

311.36

a straight out except

315.84

so let me do let's do it this way

322.72

let's just do accept

326.479

uh and then we'll just do

332.24

an error occurred whoops if i spell it

336.84

right

340.24

oh there we go

345.28

okay so now what i can do is i don't

347.44

have to if i don't know it

348.56

i can just say all right i'm either

350.16

going to

353.199

probably doesn't like that

360.08

oh so it does now

364.88

like one or more handlers

369.6

so what i'd have to do here is i can

372.639

just say

374.4

exception which is the base essentially

377.68

or base exception

386.639

well it doesn't like that

417.28

three two one i'm sorry this actually

419.759

that's a warning and not a problem so i

422.72

can go back

423.599

to this

429.039

and now if i hit a number i'm fine oh

431.919

because i'm in that while loop

433.599

if i hit a letter it's is going to say

435.52

an error occurred

438.08

and now i have to break out of this

440

thing because

442.479

i killed it so pardon me a second

445.599

so i did get into my infinite loop there

447.199

as i said here i didn't have a break on

448.84

it

450.16

so it would just catch an error and it

452

would just sit keep me in that infinite

453.599

loop

457.44

so let's do this

462.319

now it goes in order uh let me bring

466

that

466.56

screen back up real quick first um

471.199

it goes in order from top to bottom so

474

that generic

476.84

exception

479.44

let's play with that a minute so if we

481.84

go to that generic

483.12

exception

490.16

if i can type

495.599

okay

498.639

so if we do just a generic exception

502.879

let's do this so if we just do accept

505.52

here

506.4

so this is going to take anything and

507.919

then we're going to say

510

oh actually now we're going to get an

511.12

error because you're going to see that

512.479

actually

513.68

this one's going to say we're going to

514.719

get errors because we will never

516.08

actually hit

517.599

these guys it starts with it goes in

520.24

order and it says okay everything's

521.76

gonna hit this

523.519

because it is a generic however if i

526.8

shift

527.279

order then i say hey i've got these

530.08

specific ones and if i don't catch if

531.839

i've got any kind of other error

534.64

then i'm going to break out or then i'm

538.08

going to

538.64

then i'm going to handle that exception

543.04

so we'll just call it let's do uncaught

546.839

exception now another thing we can do

552.32

is let's do

558.399

uh we'll keep it like that and then

560.16

we'll just pick this one so

565.6

now let's do this so now let's do a

568.959

different try

570.88

so we're just going to try print

574.64

a new example

580.56

and let's do uh ant

585.839

345. hey let's just do that

589.76

and then that's going to blow up but

591.68

this time what we're going to do

593.2

i don't need the break is that we're

596.16

actually going to

598

take that exception whatever it is and

599.76

we're going to use it and we can call it

601.44

whatever we want but we're going to just

602.399

say as

603.44

this thing so let's just say

607.76

exception we'll do it that way because

609.12

it's a little more

611.839

and you may use e sometimes people do

614.64

that based on the other languages

615.839

they've done

617.04

so let's see what happens here

620.959

when i try and play with that one

624.88

oh that's right

628.8

so let's see so i'd break

632

out of that oh here we go

635.279

and so uh the new example see it says

638.32

invalid literal

640.16

and that's what was pre um actually i

643.44

probably should do this print um

649.839

let's do it this way so it's a little

651.36

easier to read

658.48

and we're just going to do args

663.92

and let's see how that works out okay

668.72

uh i don't know whatever that was okay

671.839

so

673.2

here we go uh where is exception message

677.44

in here

683.519

oh but see this is another oh because i

685.12

can't do that

686.8

so i'm sorry because it's not going to

691.36

oh because it's not you know i've got to

694.56

convert this to a string my mistake

696.56

so i got an exception and an exception

698.079

which really blew things up

702.399

okay let's see what that does so now

705.6

when we break out

710.079

we see so we come in a new example and

713.279

we have this exception message

716.079

that comes through because we've got it

719.2

and so you can see here this exception

720.959

message it says invalid literal

723.44

so that's the message that i got with

725.2

the base 10. so that's actually what

726.48

happened

726.959

if i look at the arguments

730

then you can see here that the only

731.68

arguments that i had for the exception

735.12

is uh just the one which is basically

738.079

just

738.399

the message but now i can do

741.519

with that actually let's do the same

743.36

thing

747.36

let's see let's do arg example

754.079

and i can do a thing called raise

760

and i want this to be um

764.24

let's see one two three

769.12

please space that just so it doesn't

770.32

complain at me

772.16

so now if i raise it's going to actually

774.639

throw an exception

776.8

and so now i can come in

784.839

and

786.16

oh here we go so then it's going to come

788

in

789.36

a new example arg example exceptions

791.6

must derive from base exception so

793.839

i can do

798.839

uh

804.72

let's do so i have to give it one and so

808.399

i'm gonna do

822.639

three two one so i want to give it

825.519

actually

826.88

in this case i have to actually tell it

828.24

i'm sorry i can't just raise i have to

829.6

tell it the exception type

833.44

and so now if i do that break out

837.279

so here my exception was this message

841.199

so i could also do um let's try like

845.44

let's do my exception

850.72

name

854.839

and oh let's see

859.68

oh that's not a name that's

863.199

my exception and it's gonna be let's see

865.04

a name and a code

872.399

and this is going to come from

876.399

this exception

901.76

three two one all right let's make this

904.639

an

904.88

exception so it does that and then what

907.44

i can do is i can do raise

912.32

my exception

916.16

is going to be

919.519

see negative 23. and

922.639

it's going to just call it we'll call it

924.839

henry

926.8

and then

935.519

three two one and let's get rid of that

939.44

let's see

940.079

this is going to be the name

943.68

and the code

949.759

and we're going to name and code

966.839

and so if we look at this one

970.72

so now we're raising a my exception

975.279

and we can actually do

978.639

we can leave it as exception we'll call

980.24

we're gonna actually raise i actually do

982.079

specifically on my exception

985.759

and let's do this

993.6

and we'll see here that the args now

995.44

come across as

997.04

that and we can actually do

1000.639

print

1003.839

name and do exception dot

1008.839

name

1011.68

actually we could do it like this we

1013.68

could do

1015.279

um so for that one

1024

we'll do it that way let's do it this

1025.36

way we'll just do this one so we can do

1026.64

name and we can do

1031.36

code

1035.52

and we can actually work with those

1037.039

individually

1043.919

ah break out of that and so we can see

1045.919

here that the name is henry and the code

1047.679

is negative 23. so we could actually do

1049.12

some pretty complicated exceptions

1051.12

if we need to and we can build on them

1053.12

we can we could log information we could

1055.2

do

1055.84

whatever we needed to but the key is

1060

for this one really is just to know that

1061.36

there's a try and accept

1063.44

you can have multiple accepts that there

1066.16

is

1067.12

a generic and essentially there's a

1069.36

catch-all which would you

1070.64

just accept this now we do have

1076.32

uh let's see

1085.28

well i think we'll go with that

1095.52

three two one but we do have one other

1098.64

thing

1099.039

and so we can do a

1102.32

uh where do i want to put it i'm gonna

1104.24

put it here

1106.32

we also have a finally

1110.16

which means regardless of what we do

1114.559

we're going to call the finale

1119.28

let me get rid of that so what we could

1120.64

do is we could say we're going to try

1121.84

this and no matter what we're going to

1123.12

break out of the loop

1124.16

something's going to happen and we're

1125.2

going to break out a loop so now if we

1127.84

do that

1129.12

we can see if we enter a number now boom

1131.76

it breaks out a loop

1133.28

if we enter a letter boom it breaks out

1135.28

a loop if we do ctrl c

1137.12

boom it breaks out a loop so this always

1139.6

gets called

1142

so we have our key pieces of exceptions

1146.4

as we have try we catch some exceptions

1148.88

we have one or more

1150.16

finally says hey we're going to do this

1152.08

no matter what whether it's a

1153.679

exception or not and then we can also

1157.28

raise

1157.84

exceptions so we can come in and do our

1160

own so we could do something like a

1162.88

like a number two low exception or you

1165.28

know value too high or something along

1166.96

those lines

1168.64

and or for example uh let's say a

1171.76

customer id we could have something we

1173.44

have a customer bad id

1176.08

exception that we raise that basically

1178.24

says if somebody says

1179.28

gives us a bad id we always do this and

1182.16

we always

1182.64

give them follow this logical path

1186.48

so that is exceptions in python and

1189.6

that'll wrap it up for this one so as

1192.24

always if you have any questions

1193.36

shoot an email to info development or

1195.2

dot com uh there will be up

1197.039

you know the repository will have

1198.799

examples of this in the

1200.6

exceptions.py file that's out there and

1203.52

you can see

1204

links in the show nut show notes but

1207.2

as always go out there have yourself a

1208.88

great day a great week

1210.64

and we will talk to you next time