d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Oracle Sql > Exception Block Stopping Code Even > When No Exceptions Are Caught?
12Next
Add Reply New Topic New Poll
Member
Posts: 1,208
Joined: Aug 1 2013
Gold: 50.00
Feb 22 2014 11:17pm
This is Oracle SQL

Title says it all; exception block is stopping successor code from running even when no exceptions are caught. Everything works perfectly with the exception block commented out... except no exceptions are being caught and output.


Code

CREATE OR REPLACE PROCEDURE
CheckInGuest(guestId INTEGER, hotelId INTEGER, numNights INTEGER, roomNum VARCHAR2) AS
url VARCHAR(100) := '"http://dboracle.eng.fau.edu:7777/~tmeares2012/SimpleMenu.HTML"';
guestName VARCHAR2(100);
hotelName VARCHAR2(100);
errMessage VARCHAR2(200);
stayId INTEGER;
BEGIN
SELECT first_name||' '||last_name INTO guestName FROM guest WHERE guest_id=guestId;
SELECT hotel_name INTO hotelName FROM hotel WHERE hotel_id=hotelId;
SELECT stay_id+1 INTO stayId FROM stay WHERE stay_id = (SELECT MAX(stay_id) FROM stay);

EXCEPTION
WHEN NO_DATA_FOUND THEN
BEGIN
errMessage := 'Invalid input parameter';
ReturnMenu(errMessage, url);
END;
WHEN OTHERS THEN
BEGIN
errMessage := 'Something bad happened - '||SQLCODE;
ReturnMenu(errMessage, url);
END;

INSERT INTO stay(stay_id, guest_id, hotel_id, start_date, number_days, room)
VALUES (stayId, guestId, hotelId, SYSDATE, numNights, roomNum);
COMMIT;

htp.print('<html>');
htp.print(' <head>');
htp.print(' <title> Guest Table </title>');
htp.print(' </head>');
htp.print(' <body>');
htp.print(' <h1> Insert Successful </h1>');
htp.print(' <p>');
htp.print(' Guest: '||guestName||' Checked In To Hotel: '||hotelName||' for '||numNights||' Nights In Room '||roomNum);
htp.print(' </p>');
-- footer --
htp.print(' <p>');
htp.print(' <a href='||url||'>');
htp.print(' click here to return to main menu');
htp.print(' </a>');
htp.print(' </p>');
htp.print(' </body>');
htp.print('</html>');
END;


Thank you :LOVE:
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 22 2014 11:44pm
Don't use BEGIN/END inside the WHEN clauses.

This post was edited by carteblanche on Feb 22 2014 11:46pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 22 2014 11:52pm
Your exception handlers is for the main begin. If it catches an error it is going to try to return out of it. If you want it to catch whatever errors occur, and then continue on afterwards you need to wrap everything you want exception handled within its own begin.

Something like this

Code
CREATE OR REPLACE PROCEDURE
CheckInGuest(guestId INTEGER, hotelId INTEGER, numNights INTEGER, roomNum VARCHAR2) AS
url VARCHAR(100) := '"http://dboracle.eng.fau.edu:7777/~tmeares2012/SimpleMenu.HTML"';
guestName VARCHAR2(100);
hotelName VARCHAR2(100);
errMessage VARCHAR2(200);
stayId INTEGER;
BEGIN
BEGIN
SELECT first_name||' '||last_name INTO guestName FROM guest WHERE guest_id=guestId;
SELECT hotel_name INTO hotelName FROM hotel WHERE hotel_id=hotelId;
SELECT stay_id+1 INTO stayId FROM stay WHERE stay_id = (SELECT MAX(stay_id) FROM stay);

EXCEPTION
WHEN NO_DATA_FOUND THEN
errMessage := 'Invalid input parameter';
ReturnMenu(errMessage, url);
WHEN OTHERS THEN
errMessage := 'Something bad happened - '||SQLCODE;
ReturnMenu(errMessage, url);
END;

INSERT INTO stay(stay_id, guest_id, hotel_id, start_date, number_days, room)
VALUES (stayId, guestId, hotelId, SYSDATE, numNights, roomNum);
COMMIT;

htp.print('<html>');
htp.print(' <head>');
htp.print(' <title> Guest Table </title>');
htp.print(' </head>');
htp.print(' <body>');
htp.print(' <h1> Insert Successful </h1>');
htp.print(' <p>');
htp.print(' Guest: '||guestName||' Checked In To Hotel: '||hotelName||' for '||numNights||' Nights In Room '||roomNum);
htp.print(' </p>');
-- footer --
htp.print(' <p>');
htp.print(' <a href='||url||'>');
htp.print(' click here to return to main menu');
htp.print(' </a>');
htp.print(' </p>');
htp.print(' </body>');
htp.print('</html>');
END;


I also removed the BEGIN/END out of the WHEN clauses. Not sure why you had them there


This post was edited by Minkomonster on Feb 22 2014 11:53pm
Member
Posts: 1,208
Joined: Aug 1 2013
Gold: 50.00
Feb 23 2014 12:14am
Quote (Minkomonster @ Feb 23 2014 01:52am)
Your exception handlers is for the main begin. If it catches an error it is going to try to return out of it. If you want it to catch whatever errors occur, and then continue on afterwards you need to wrap everything you want exception handled within its own begin.

Something like this

I also removed the BEGIN/END out of the WHEN clauses. Not sure why you had them there


That's how my teacher showed us Exceptions.... :| I did try removing the BEGIN/END's and used just one line but it still did the same.


Thank you for quick responces, trying your suggestions now. :LOVE:
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 23 2014 12:33am
Did you wrap the statements you wanted exception handling on in their own begin/end like I suggested?
Member
Posts: 1,208
Joined: Aug 1 2013
Gold: 50.00
Feb 23 2014 12:35am
Quote (carteblanche @ Feb 23 2014 01:44am)
Don't use BEGIN/END inside the WHEN clauses.


How exactly do you know EVERY language? xD



Quote (Minkomonster @ Feb 23 2014 01:52am)
Your exception handlers is for the main begin. If it catches an error it is going to try to return out of it. If you want it to catch whatever errors occur, and then continue on afterwards you need to wrap everything you want exception handled within its own begin.

Something like this


I tried your example (and a few other things) and it doesn't seem to handle the errors the way we were showed to, but I think that's because I didn't explain it properly. The procedure "ReturnMenu" inside of the exception handling links and generates a new webpage that displays the message the errMessage and links to the other pages. Problem is when the Exception is in the nested BEGIN/END, it doesn't seem to do the ReturnMenu.

I was trying to run the ReturnMenu and stop if an exception was caught, but continue on to the HTML below if no exceptions are caught.


This post was edited by SanityWasHacked on Feb 23 2014 12:37am
Member
Posts: 1,208
Joined: Aug 1 2013
Gold: 50.00
Feb 23 2014 01:55am
Got it to work by putting the exception block at the very end of the script. ^_^

This post was edited by SanityWasHacked on Feb 23 2014 01:55am
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 23 2014 02:51am
Quote (SanityWasHacked @ Feb 23 2014 02:55am)
Got it to work by putting the exception block at the very end of the script. ^_^


Thats...what I told you to do. Your exception block was terminating early. You either needed it at the end of the block, or you needed to wrap it in its own begin/end if you wanted local exception handling.
Member
Posts: 1,208
Joined: Aug 1 2013
Gold: 50.00
Feb 23 2014 05:34am
Quote (Minkomonster @ Feb 23 2014 04:51am)
Thats...what I told you to do. Your exception block was terminating early. You either needed it at the end of the block, or you needed to wrap it in its own begin/end if you wanted local exception handling.


I understand that now, but you never directly said putting it at the end would fix it. I'm obviously very new to this stuff so I apologize for not grasping your implications.

However, handling the error at the end doesn't prevent it from entering invalid data at the insert which is why I originally put it before that, and wrapping it in it's own begin/end at the start wasn't handling the errors the way I wanted, so it's pretty much a compromise at this point. I'll just throw in a delete statement in the invalid handler and call it a day. :rolleyes:


Again, thank you for the help. <3


e/ if there is a more proper way to approach this and you don't mind elaborating, please feel free to educate me. I willl take your words to heart.


This post was edited by SanityWasHacked on Feb 23 2014 05:40am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 24 2014 06:57pm
Quote (SanityWasHacked @ Feb 23 2014 01:35am)
How exactly do you know EVERY language? xD


actually, i don't know ANY language. i just google everyone's problem and read the solution. or if it's a compiler/runtime error, i just read the error. newbies can't tell the difference. just don't tell anyone, eh?
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll