📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Exceptions

2022-05-26 •Youtube

Detailed Notes

1. Exceptions

DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN ROLLBACK; SELECT 'An error has occurred, work rolled back, procedure terminated'; END;

DECLARE CONTINUE HANDLER FOR NOT FOUND SELECT 'Row Not Found';

create table some_values( id INT NOT NULL AUTO_INCREMENT, value1 VARCHAR(10) NOT NULL, value2 VARCHAR(10) NOT NULL, PRIMARY KEY ( id ) ); DELIMITER //

CREATE OR REPLACE PROCEDURE simpleException ( in _value varchar(10) ) sp: BEGIN DECLARE idCount INT DEFAULT 0;

DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN ROLLBACK; SELECT 'An error has occurred, work rolled back, procedure terminated' as 'Message'; END;

select 'Inserting Record...' as 'Message'; insert into some_values (value1,value2) values ('first',_value); END//

DELIMITER ;

DELIMITER //

CREATE OR REPLACE PROCEDURE simpleContinue ( in _id int ) BEGIN

DECLARE CONTINUE HANDLER FOR 1364 SELECT 'Unable to insert row' as 'Message';

insert into app_user(t_parent_id) values (_id); SELECT 'Complete' as 'Message'; END//

DELIMITER ; insert into app_user(t_parent_id,email,userLogin) values (1,'',''); DELIMITER //

Transcript Text
[Music]
well hello and welcome back we're
continuing looking at our going through
our sql tutorials and focus mostly on
mysql and mariadb
today we're continuing uh it's more or
less
focused in the world of stored
procedures right now as we have been the
last few sessions but we're going to
look at exceptions and error handling
which may show up in general scripts as
well
now let's start with
exceptions
so with an exception
you basically have two types of handlers
that you're working with it's either
going to be exit which means if i get an
exception i am going to bail out of this
or continue which means if i get this
exception i am going to continue moving
forward i'm going to continue with my
script so let me go back to
let's take a look at one of these
uh let's see
let's pick one from yesterday
[Music]
let's do this one
and we will modify this guy a little bit
so we're gonna take this leave me
procedure that we did and we're gonna do
create
or replace
and this time
uh instead of leave
we're just gonna do
um
let's do an exit handler
and so we're gonna take that
so this is where if we given an id and
it doesn't exist
then we want to do something special so
here
we're going to do it and
this is gonna be now what we're gonna
need here is an actual exception so
let's do
um
let's change this around a little bit
so in this case instead of the if
what we're going to do is we're going to
do this count actually we don't even
need to do the count
uh let me do we're going to call this
leave me 2.
and let's just
change the whole thing up so we're going
to call this a
simple exception
and we're going to give an id and
instead what we're going to do here is
we don't even need to do that
what we're going to do is we're going to
clear this and we're going to say errors
occurred and then we're going to just
update
app user
and we're going to set a value let's see
let's look at
um
let me jump into my database here
and let's do select star from
app user
let's update
uh we're going to update the user login
let's just set it well let's just start
here okay let's do that one so we're
going to set start
year equal to uh 2020
we're
that so what we're going to do is we're
going to end up generating an exception
here because if i do this
and it is not
uh let's do parent id equals zero then
oh interesting it does not throw that
it does not give me so we're gonna have
to change that up a little bit uh
describe
all right i may have to create a new
table here real quick
yeah because all mine are way too easy
so here let's do this
uh let's go back to i think i can steal
some great tables so what i'm gonna do
here
is
[Music]
somewhere back here i've got here we go
let's do this
so what we're going to do is we're going
to come in and we're going to
before that okay
going to create table
some values
and it's going to have an id
and
it's going to have let's just do
value 1
cannot be null
value 2
no primary key is going to be id
okay so we're going to take that
and we create this so now we can't
insert so if we go into uh
into
some values
value
1
values
first
no it should generate there we go okay
so now it's going to get that so let's
take
let's make sure we got those guys
here in our
apologies we'll do this let's take that
insert
i'll take that guy
and that's going to be in our procedure
here
okay so we're coming here
that's all good we've got him created so
let's take
a simple exception
there we go so i'm going to put him
there let's copy that here first
was that what i want oh well i've just
got all kinds of random stuff in here so
let's just do it like that
and now
okay so now i have simple exception
and if i give it
oh i guess it doesn't really matter what
i can give it any number it doesn't
matter at this point
and so here
i get that there was an exception there
now it's going to try that
but if i do so let's start from
some values
i see that it didn't come in
and instead i get this and notice that
this is
you know the text is also the column so
what i could do is
uh and this will roll back here and so
that rollback doesn't really matter too
much but if i do
let's call him message
well i don't want to say that there but
i do want to do it here
like that
now if i go back to my call
uh
there we go
now i'm gonna see where okay so i can do
it just like anything else i'm gonna
list that out here's the message
and i can display this out and this can
occur when there's an exception if i say
this if i do value
to
values first
comma underscore value
and change this
up
is that right oop let me get that from
here
ah darn it come here
drag and drop it's not always awesome
so now if i do it and i do call simple
exception
if i call it with
my value
then it's going to work
if i send it a null
then it's going to say no can't do it
because that's a null and i can do other
checks around this i could
um well this is a sql exception so
here i'm not really doing necessarily
some
issues with this or i'm not i have to
have this
i have to have an exception actually get
raised
now
same thing is for so this is an exit
handler
uh is it go oh let me do this
let's try this select
uh
as
message
inserting record
one two three okay so let's do that oh
darn it i want to
save that there i do want to do that
let's go in here
and let's do like a later version of
that
okay so
now oh
i don't want to do that okay
so now if i call
simplexception and i call with a null
then i see that there's oh it does come
through and do that and then it throws
that exception oh
i'm sorry
insert tid
because it's not going to generate the
exception until
i try to do the insert my mistake so
let's take that
and do it here
did i just do that
there okay let's try it again oh
and can i just call that again call call
call where'd he go
oh i guess not
it's easier to do it this way so now if
i call it with a null
i just get that that error
if i call with a value
then it does come through and says hey i
answered the record because it comes
through here now if i do a continue
which is why i did all of this
which i can go up
and so if i go up here
and instead of an exit handler i do a
contender continue handler and
let me actually do it for that one so
now let's say
let's change this around a little bit um
let's do
uh
let's take that whole thing
and now i'm going to do a simple
continue
and this one instead what i'm going to
do
is let's get back to that one that i had
before so i'm going to take an id
which is an ant
he comes in and then let's see if i can
find that code that i had from earlier
let's do this
actually i can just do that update now
so i can do
uh where did i just put that simple
continue here we go okay
so here
i'm gonna do
update
app user
set
uh what did i call that
so now i should be able to see row not
found
uh let's see
start here
start here
equals 2010
where t parent
p
equals
underscore id
okay
up give it my close my line
i'm going to declare a continue handler
which will make this really easy
and
let's see someone continue do that so
now
i should get a row not found
uh i don't need an id count i don't
actually need that label
simple continue blah blah blah blah blah
blah blah blah let's see if this works
oops
my delimiters
um
and let's just do
select
here
as
message
uh let's do that
let's change it up just a little bit uh
let's see delimiter delimiter
let's make sure i've got it in my little
samples here
and
let's load it up okay so now i can do i
can call simple continue
if i call it with a 4
then i get there because i think
4 is there okay and it got set if i call
it with a 2
up and it's still like it because it
does not generate the row not found so
i've got to get a
a not found here
so we have to figure out how to do that
where it's going to generate it
okay i'm going to have to change this
around a little bit so enter into
app user
uh let's see
here
ct parent one let's just do this
t parent
id
values
id
and let's just do this um
let's do this first so if i do one
it should generate okay so he's going to
generate
1364. let's just do it from here
so for 13.64
as
message
and we're just going to unable to insert
row
and
uh
and we can do it this way so let's just
change this here
i apologize because we are not going to
get to error handling today we're just
going to deal with exceptions
i thought i would get a little further i
did not oops
did you
don't need you
okay
so if i call
let's just call it simple continue
then
oh i need to give it an argument
and so i'm going to get that unable to
insert row but it continues in anyways
it's still going to try to do something
oh let me put that message
is that the one i just did
nope that's not the one i did
okay wait wait wait
okay let's get rid of you let's get down
here to
where was my
here we go this guy
oops
so this
even though we're going to blow up here
we're going to do this
and we're going to come in we're going
to run this guy and then if we do that
call
call it with that
now we're gonna see that's complete even
though we got the unable to insert row
because
we have this continue handler
and so we have two different types of
handlers
that we can work with and all that stuff
yet
that we can work with that we have
talked about today
that allow us to
either exit based on an error or
you know like here we did for just a
general sql exception but we can also do
it for here this is an error code which
we picked up
when we tried to do
that sierra code right there so if i
take this same thing
and i could do it for multiples so i
could do something different depending
on what i did so if i do uh what did
that say so it needed to be email
and let's just give it this then what
it's going to give me is something
different
so now it's gonna oh okay that's still a
1364 because it's user login but
if i go here at some point it's going to
give me a different one i'm assuming
of course watch this could take me a
while to get to the error i want
it's still a 13 okay that's just a bunch
of pressure default but the point is
you'll get different error codes
and you can check for those and you
could actually have different error
handlers depending on what your error
code is so if i if i have a different
one that is like uh
um
maybe like walking to some of the other
things like divide by zero or something
like that you can generate different
errors and you can trap for those
essentially through your declares
and
that will allow you to do some there's
some basic exception handling
right a little long so next time around
we're continuing this and we're going to
look at our error
handling instead of our exceptions
and
where did that go here we go
over here somewhere there we go
so instead of air handling we're going
to take care of that next time around
and i think you'll see that it's be
it'll continue um some rather
interesting things that we can do within
our store procedures that being said
sorry if we ran a little bit long
playing around with these uh sometimes
these examples get a little complex and
sometimes
they haven't all been run through
beforehand so
my apologies but that being said go out
there and have yourself a great day a
great week and we will talk to you
next time
[Music]
you
Transcript Segments
0.43

[Music]

26.96

well hello and welcome back we're

28.64

continuing looking at our going through

30.8

our sql tutorials and focus mostly on

33.04

mysql and mariadb

35.44

today we're continuing uh it's more or

38.399

less

39.2

focused in the world of stored

40.559

procedures right now as we have been the

42.32

last few sessions but we're going to

44.719

look at exceptions and error handling

47.36

which may show up in general scripts as

50.16

well

51.68

now let's start with

53.36

exceptions

54.64

so with an exception

57.199

you basically have two types of handlers

59.68

that you're working with it's either

61.039

going to be exit which means if i get an

63.039

exception i am going to bail out of this

65.199

or continue which means if i get this

67.92

exception i am going to continue moving

70.84

forward i'm going to continue with my

72.88

script so let me go back to

75.6

let's take a look at one of these

79.6

uh let's see

82

let's pick one from yesterday

84.23

[Music]

85.28

let's do this one

89.2

and we will modify this guy a little bit

92.96

so we're gonna take this leave me

94.799

procedure that we did and we're gonna do

96.72

create

98

or replace

99.439

and this time

100.88

uh instead of leave

103.439

we're just gonna do

105.36

um

106.64

let's do an exit handler

111.04

and so we're gonna take that

113.759

so this is where if we given an id and

116.159

it doesn't exist

118.079

then we want to do something special so

120.399

here

126.24

we're going to do it and

130.56

this is gonna be now what we're gonna

132.4

need here is an actual exception so

135.04

let's do

139.36

um

140.319

let's change this around a little bit

142.64

so in this case instead of the if

147.68

what we're going to do is we're going to

149.599

do this count actually we don't even

150.8

need to do the count

152.72

uh let me do we're going to call this

154.16

leave me 2.

156.16

and let's just

157.519

change the whole thing up so we're going

159.2

to call this a

161.92

simple exception

166.08

and we're going to give an id and

167.2

instead what we're going to do here is

168.72

we don't even need to do that

171.84

what we're going to do is we're going to

172.879

clear this and we're going to say errors

174.4

occurred and then we're going to just

176.72

update

180.08

app user

182.159

and we're going to set a value let's see

183.92

let's look at

185.84

um

189.68

let me jump into my database here

196.08

and let's do select star from

198.959

app user

201.519

let's update

204.239

uh we're going to update the user login

205.76

let's just set it well let's just start

207.04

here okay let's do that one so we're

208.4

going to set start

210.319

year equal to uh 2020

213.599

we're

214.319

that so what we're going to do is we're

215.68

going to end up generating an exception

217.44

here because if i do this

220

and it is not

223.68

uh let's do parent id equals zero then

228.799

oh interesting it does not throw that

233.76

it does not give me so we're gonna have

235.28

to change that up a little bit uh

236.799

describe

238.879

all right i may have to create a new

240.48

table here real quick

242.319

yeah because all mine are way too easy

245.84

so here let's do this

247.92

uh let's go back to i think i can steal

250.72

some great tables so what i'm gonna do

252.48

here

253.68

is

254.31

[Music]

255.599

somewhere back here i've got here we go

258.88

let's do this

260.799

so what we're going to do is we're going

262

to come in and we're going to

264.56

before that okay

268

going to create table

273.68

some values

275.199

and it's going to have an id

278.8

and

279.759

it's going to have let's just do

283.6

value 1

286.4

cannot be null

289.36

value 2

296

no primary key is going to be id

299.36

okay so we're going to take that

304.8

and we create this so now we can't

306.479

insert so if we go into uh

309.84

into

313.28

some values

316.479

value

317.759

1

319.36

values

323.12

first

324.88

no it should generate there we go okay

326.8

so now it's going to get that so let's

328.639

take

329.759

let's make sure we got those guys

333.199

here in our

339.639

apologies we'll do this let's take that

341.919

insert

344.08

i'll take that guy

348.639

and that's going to be in our procedure

351.12

here

355.28

okay so we're coming here

357.6

that's all good we've got him created so

359.6

let's take

361.28

a simple exception

366.16

there we go so i'm going to put him

367.759

there let's copy that here first

377.199

was that what i want oh well i've just

379.039

got all kinds of random stuff in here so

380.56

let's just do it like that

382.88

and now

387.36

okay so now i have simple exception

393.039

and if i give it

396.16

oh i guess it doesn't really matter what

397.52

i can give it any number it doesn't

398.96

matter at this point

401.84

and so here

406.8

i get that there was an exception there

408.479

now it's going to try that

410.56

but if i do so let's start from

416.96

some values

419.84

i see that it didn't come in

421.599

and instead i get this and notice that

423.44

this is

424.56

you know the text is also the column so

426.4

what i could do is

429.199

uh and this will roll back here and so

431.28

that rollback doesn't really matter too

433.199

much but if i do

441.28

let's call him message

450.24

well i don't want to say that there but

451.919

i do want to do it here

456.24

like that

460

now if i go back to my call

463.199

uh

464.16

there we go

465.28

now i'm gonna see where okay so i can do

467.039

it just like anything else i'm gonna

468.24

list that out here's the message

470.319

and i can display this out and this can

472

occur when there's an exception if i say

476.96

this if i do value

480.08

to

480.96

values first

482.639

comma underscore value

484.96

and change this

499.919

up

507.199

is that right oop let me get that from

509.199

here

510.639

ah darn it come here

514

drag and drop it's not always awesome

516.24

so now if i do it and i do call simple

519.2

exception

521.919

if i call it with

524.48

my value

526.48

then it's going to work

528.48

if i send it a null

531.6

then it's going to say no can't do it

533.44

because that's a null and i can do other

534.959

checks around this i could

537.44

um well this is a sql exception so

540.56

here i'm not really doing necessarily

542.399

some

543.36

issues with this or i'm not i have to

545.839

have this

547.6

i have to have an exception actually get

549.12

raised

550.48

now

551.279

same thing is for so this is an exit

554.24

handler

555.36

uh is it go oh let me do this

557.92

let's try this select

562.64

uh

563.519

as

565.92

message

570.24

inserting record

572.72

one two three okay so let's do that oh

575.12

darn it i want to

577.279

save that there i do want to do that

581.92

let's go in here

584.16

and let's do like a later version of

585.92

that

587.6

okay so

588.839

now oh

591.839

i don't want to do that okay

595.519

so now if i call

598.44

simplexception and i call with a null

602.16

then i see that there's oh it does come

604.72

through and do that and then it throws

606.24

that exception oh

608.399

i'm sorry

611.68

insert tid

616.72

because it's not going to generate the

618

exception until

619.6

i try to do the insert my mistake so

621.519

let's take that

626.88

and do it here

631.76

did i just do that

633.519

there okay let's try it again oh

636.64

and can i just call that again call call

639.04

call where'd he go

641.04

oh i guess not

643.519

it's easier to do it this way so now if

645.92

i call it with a null

648.8

i just get that that error

651.04

if i call with a value

655.76

then it does come through and says hey i

657.279

answered the record because it comes

658.64

through here now if i do a continue

661.2

which is why i did all of this

663.36

which i can go up

666.16

and so if i go up here

668.72

and instead of an exit handler i do a

670.8

contender continue handler and

675.2

let me actually do it for that one so

676.88

now let's say

681.519

let's change this around a little bit um

683.92

let's do

686.16

uh

692.24

let's take that whole thing

697.12

and now i'm going to do a simple

698.88

continue

702.959

and this one instead what i'm going to

705.36

do

706.56

is let's get back to that one that i had

708.24

before so i'm going to take an id

710.399

which is an ant

713.44

he comes in and then let's see if i can

715.279

find that code that i had from earlier

722

let's do this

726.72

actually i can just do that update now

728.48

so i can do

735.12

uh where did i just put that simple

737.44

continue here we go okay

742.48

so here

748.56

i'm gonna do

753.44

update

755.68

app user

758.24

set

759.12

uh what did i call that

763.6

so now i should be able to see row not

765.44

found

767.12

uh let's see

768.959

start here

773.2

start here

776.48

equals 2010

778.8

where t parent

781.44

p

782.16

equals

783.279

underscore id

785.6

okay

786.8

up give it my close my line

790.16

i'm going to declare a continue handler

792.48

which will make this really easy

798.32

and

799.44

let's see someone continue do that so

802

now

805.12

i should get a row not found

808.399

uh i don't need an id count i don't

810.079

actually need that label

812.079

simple continue blah blah blah blah blah

814.16

blah blah blah let's see if this works

815.92

oops

816.88

my delimiters

822

um

823.519

and let's just do

825.92

select

829.92

here

832.48

as

833.76

message

836.24

uh let's do that

837.76

let's change it up just a little bit uh

839.92

let's see delimiter delimiter

842.639

let's make sure i've got it in my little

844.079

samples here

850.32

and

851.76

let's load it up okay so now i can do i

854.639

can call simple continue

858.48

if i call it with a 4

861.6

then i get there because i think

867.6

4 is there okay and it got set if i call

869.6

it with a 2

873.44

up and it's still like it because it

875.199

does not generate the row not found so

877.6

i've got to get a

880.56

a not found here

882.8

so we have to figure out how to do that

884.48

where it's going to generate it

886.639

okay i'm going to have to change this

887.92

around a little bit so enter into

890.56

app user

893.76

uh let's see

895.199

here

896.16

ct parent one let's just do this

898.88

t parent

902.16

id

909.279

values

915.92

id

916.88

and let's just do this um

921.04

let's do this first so if i do one

924.56

it should generate okay so he's going to

926.399

generate

928.16

1364. let's just do it from here

931.92

so for 13.64

937.68

as

938.959

message

945.44

and we're just going to unable to insert

947.839

row

951.68

and

952.8

uh

955.839

and we can do it this way so let's just

957.6

change this here

961.279

i apologize because we are not going to

962.88

get to error handling today we're just

964.399

going to deal with exceptions

966.8

i thought i would get a little further i

968.079

did not oops

970

did you

971.279

don't need you

972.8

okay

973.92

so if i call

975.36

let's just call it simple continue

981.6

then

984.24

oh i need to give it an argument

989.36

and so i'm going to get that unable to

991.12

insert row but it continues in anyways

994.079

it's still going to try to do something

995.199

oh let me put that message

1007.759

is that the one i just did

1009.68

nope that's not the one i did

1014.079

okay wait wait wait

1019.199

okay let's get rid of you let's get down

1021.36

here to

1024.24

where was my

1025.6

here we go this guy

1030.48

oops

1032.48

so this

1033.52

even though we're going to blow up here

1040

we're going to do this

1044.16

and we're going to come in we're going

1045.839

to run this guy and then if we do that

1047.919

call

1049.76

call it with that

1051.2

now we're gonna see that's complete even

1053.2

though we got the unable to insert row

1055.679

because

1056.799

we have this continue handler

1059.44

and so we have two different types of

1061.84

handlers

1063.12

that we can work with and all that stuff

1064.88

yet

1066.48

that we can work with that we have

1067.84

talked about today

1070.08

that allow us to

1072

either exit based on an error or

1076.559

you know like here we did for just a

1078.08

general sql exception but we can also do

1079.76

it for here this is an error code which

1082

we picked up

1083.84

when we tried to do

1086.799

that sierra code right there so if i

1088.96

take this same thing

1093.28

and i could do it for multiples so i

1095.2

could do something different depending

1096.4

on what i did so if i do uh what did

1098.48

that say so it needed to be email

1103.12

and let's just give it this then what

1104.88

it's going to give me is something

1105.919

different

1108.48

so now it's gonna oh okay that's still a

1110.559

1364 because it's user login but

1114.24

if i go here at some point it's going to

1116.08

give me a different one i'm assuming

1120

of course watch this could take me a

1121.12

while to get to the error i want

1125.52

it's still a 13 okay that's just a bunch

1127.36

of pressure default but the point is

1130.799

you'll get different error codes

1133.28

and you can check for those and you

1135.44

could actually have different error

1136.64

handlers depending on what your error

1137.919

code is so if i if i have a different

1139.679

one that is like uh

1142.16

um

1143.36

maybe like walking to some of the other

1145.039

things like divide by zero or something

1146.88

like that you can generate different

1148.64

errors and you can trap for those

1150.4

essentially through your declares

1152.72

and

1153.6

that will allow you to do some there's

1155.76

some basic exception handling

1157.76

right a little long so next time around

1159.44

we're continuing this and we're going to

1160.72

look at our error

1162.64

handling instead of our exceptions

1166.16

and

1167.52

where did that go here we go

1169.76

over here somewhere there we go

1172.88

so instead of air handling we're going

1174.08

to take care of that next time around

1175.919

and i think you'll see that it's be

1177.679

it'll continue um some rather

1179.6

interesting things that we can do within

1181.6

our store procedures that being said

1184.16

sorry if we ran a little bit long

1185.36

playing around with these uh sometimes

1187.039

these examples get a little complex and

1189.12

sometimes

1190.16

they haven't all been run through

1191.52

beforehand so

1193.6

my apologies but that being said go out

1196.32

there and have yourself a great day a

1198

great week and we will talk to you

1200.799

next time

1202.54

[Music]

1218.08

you