Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Mailing Lists -> Oracle-L -> Re: Detecting the line of the Error in a PL/SQL Code
This is a multi-part message in MIME format.
------=_NextPart_000_003F_01C07649.4797E140 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Hi Peter
The 'Eleant' (or certainly correct way) to write the code is in anonymous P= L/SQL blocks. So the correct stucture of the code segment you gave is (in = order to hadle the errors elagantly) is
BEGIN -- of main block
BEGIN -- of block for first Select
SELECT ...
EXCEPTION -- Exception handler for first Select
WHEN NO_DATA_FOUND THEN=20 INSERT INTO errors VALUES ('Error in First Select statement ' || E=rror Code & Number );=20
END; -- of block for first Select
BEGIN -- of block for second Select
SELECT ...
EXCEPTION -- Exception handler for second Select
WHEN NO_DATA_FOUND THEN=20 INSERT INTO errors VALUES ('Error in Second Select statement ' || =Error Code & Number );=20
END; -- of block for second Select
BEGIN -- of block for Third Select
SELECT ...
EXCEPTION -- Exception handler for Third Select
WHEN NO_DATA_FOUND THEN=20 INSERT INTO errors VALUES ('Error in Third Select statement ' || E=rror Code & Number );=20
END; -- of block for Third Select
END; -- of main block
Hope this helps ..
Regards
Oweson Flynn
Hi Gurus!=20
I've a very interesting question about the Error Handling in the ORACLE.=
Is there a better way to detect the line of the statement which cause the= Error than insert a lot of 'variable :=3D something' like statement in th= e code....=20
I've found the following guidelies in the oracle documentation, which sug= gest that solution:=20
Exceptions can mask the statement that caused an error, as the following = example shows:=20
BEGIN=20
SELECT ...=20 SELECT ...=20 SELECT ...=20 ...=20 EXCEPTION=20 WHEN NO_DATA_FOUND THEN ...=20 -- Which SELECT statement caused the error?=20END;=20 Normally, this is not a problem. But, if the need arises, you can use a l= ocator variable to track statement execution, as follows:=20
DECLARE=20
stmt INTEGER :=3D 1; -- designates 1st SELECT statement=20
BEGIN=20
SELECT ...=20 stmt :=3D 2; -- designates 2nd SELECT statement=20 SELECT ...=20 stmt :=3D 3; -- designates 3rd SELECT statement=20 SELECT ...=20 ...=20 EXCEPTION=20 WHEN NO_DATA_FOUND THEN=20 INSERT INTO errors VALUES ('Error in statement ' || stmt);=20 ...=20
Is there any other built in function which can solve this problem, becaus=
e this is not a nice solution?=20
Thank you for your helps in advance....=20
Peter Sipka=20
This message may contain information which is confidential and subject to l= egal privilege. If you are not the intended recipient, you may not peruse, = use, disseminate, distribute or copy this message. If you have received thi= s message in error, please notify the sender immediately by email, facsimil=e or telephone and return and/or destroy the original message.
------=_NextPart_000_003F_01C07649.4797E140 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META http-equiv=3DContent-Type content=3D"text/html; charset=3Diso-8859-1"> <META content=3D"MSHTML 5.50.4134.600" name=3DGENERATOR> <STYLE></STYLE> </HEAD> <BODY bgColor=3D#ffffff> <DIV><FONT size=3D2>Hi Peter</FONT></DIV> <DIV><FONT size=3D2></FONT> </DIV> <DIV><FONT size=3D2>The 'Eleant' (or certainly correct way) to write the co=de is=20
<DIV><FONT size=3D2></FONT> </DIV> <DIV><FONT size=3D2>BEGIN -- of main block</FONT></DIV> <DIV><FONT size=3D2></FONT> </DIV> <DIV><FONT size=3D2> BEGIN -- of block for first Select</FONT><= /DIV>
<DIV><FONT size=3D2></FONT> </DIV> <DIV><FONT size=3D2> <DIV><FONT size=3D2> -- other code</FONT></DIV> <DIV> </DIV> <DIV><FONT size=3D2> BEGIN -- of block for second Select</FONT>= </DIV> <DIV><FONT size=3D2> SELECT ...</FONT></DIV> <DIV><FONT size=3D2> EXCEPTION -- Exception handler for se=cond=20
<DIV><FONT size=3D2> <DIV><FONT size=3D2></FONT> </DIV> <DIV><FONT size=3D2> <DIV><FONT size=3D2> -- other code</FONT></DIV> <DIV> </DIV></FONT></DIV></FONT></DIV> <DIV><FONT size=3D2> BEGIN -- of block for Third Select</FONT><= /DIV>
<DIV> <DIV><FONT size=3D2></FONT> </DIV> <DIV><FONT size=3D2> <DIV><FONT size=3D2> -- other code</FONT></DIV> <DIV> </DIV></FONT></DIV></DIV>END; -- of main=20 block</FONT></DIV> <DIV><FONT size=3D2></FONT> </DIV> <DIV><FONT size=3D2>Hope this helps ..</FONT></DIV><DIV><FONT size=3D2></FONT> </DIV>
Flynn<BR>------------------------------------------------------------------=<BR>Certified=20
suggest that solution:=20
<P><FONT size=3D-1>Exceptions can mask the statement that caused an error=
, as=20
the following example shows:</FONT><FONT size=3D-1></FONT>=20
<P><FONT size=3D-1>BEGIN</FONT> <BR><FONT size=3D-1> SELECT .= ..</FONT>=20 <BR><FONT size=3D-1> SELECT ...</FONT> <BR><FONT=20size=3D-1> SELECT ...</FONT> <BR><FONT size=3D-1> =
...</FONT> <BR><FONT size=3D-1>EXCEPTION</FONT> <BR><FONT size=3D-1> =
; =20
WHEN NO_DATA_FOUND THEN ...</FONT> <BR><FONT=20
size=3D-1> -- Which SELECT statement caused=
the=20
error?</FONT> <BR><FONT size=3D-1>END;</FONT><FONT size=3D-1></FONT>=20
<P><FONT size=3D-1>Normally, this is not a problem. But, if the need aris=
es, you=20
can use a locator variable to track statement execution, as=20
follows:</FONT><FONT size=3D-1></FONT>=20
<P><FONT size=3D-1>DECLARE</FONT> <BR><FONT size=3D-1> stmt I=
NTEGER :=3D=20
1; -- designates 1st SELECT statement</FONT> <BR><FONT=20
size=3D-1>BEGIN</FONT> <BR><FONT size=3D-1> SELECT ...</FONT>=
<BR><FONT size=3D-1> stmt :=3D 2; -- designates 2nd SEL=
ECT=20
statement</FONT> <BR><FONT size=3D-1> SELECT ...</FONT> <BR><=
FONT=20
size=3D-1> stmt :=3D 3; -- designates 3rd SELECT=20
statement</FONT> <BR><FONT size=3D-1> SELECT ...</FONT> <BR><=
FONT=20
size=3D-1> ...</FONT> <BR><FONT size=3D-1>EXCEPTION</FONT> <B=
R><FONT=20
size=3D-1> WHEN NO_DATA_FOUND THEN</FONT> <BR><FONT=20
size=3D-1> INSERT INTO errors VALUES ('Erro=
r in=20
statement ' || stmt);</FONT> <BR><FONT size=3D-1> =
=20
...</FONT> <BR><FONT size=3D-1>END;</FONT> <BR> =20
<P>Is there any other built in function which can solve this problem, bec=
ause=20
this is not a nice solution? <BR>Thank you for your helps in advance....=
<P> Peter Sipka </P></BLOCKQUOTE><CODE><FONT SIZE=3D3><=
BR>
<BR>
********************************************************************<BR><BR>
This message may contain information which is confidential and subject to l= egal privilege. If you are not the intended recipient, you may not peruse, = use, disseminate, distribute or copy this message. If you have received thi= s message in error, please notify the sender immediately by email, facsimil=Received on Thu Jan 04 2001 - 04:24:27 CST
![]() |
![]() |