我尝试在过程未插入记录时引发异常。我的代码如下:
CREATE OR REPLACE PROCEDURE OPEN_CLASS(
p_class IN TUTPRAC.CLASSID%TYPE,
p_unitc IN TUTPRAC.UNITCODE%TYPE,
p_classd IN TUTPRAC.CLASS_DAY%TYPE,
p_classt IN TUTPRAC.CLASS_TIME%TYPE,
p_classtp IN TUTPRAC.CLASS_TYPE%TYPE,
p_roomnm IN TUTPRAC.ROOMNUM%TYPE)
IS
-- Variables
x number:=0;
y number:=0;
CLASS_CLASH EXCEPTION;
BEGIN
-- checks
SELECT nvl((SELECT 1
FROM TUTPRAC
WHERE UNITCODE = p_unitc and CLASS_DAY = p_classd
or CLASS_DAY = p_classd and CLASS_TIME = p_classt
and ROOMNUM = p_roomnm) , 0)
INTO x FROM dual;
SELECT nvl((SELECT 1
FROM UNITSTREAM
WHERE UNITCODE = p_unitc and DAY = p_classd
or DAY = p_classd and TIME = p_classt
and LOCATION = p_roomnm) , 0)
INTO y FROM dual;
-- insert
IF (x = 0 and y = 0) THEN
INSERT INTO TUTPRAC (CLASSID, UNITCODE, CLASS_DAY, CLASS_TIME, CLASS_TYPE, ROOMNUM)
VALUES (p_class, p_unitc, p_classd, p_classt, p_classtp, p_roomnm);
ELSE
RAISE CLASS_CLASH;
END IF;
EXCEPTION
WHEN CLASS_CLASH THEN
DBMS_OUTPUT.PUT_LINE('Record was not inserted due to a class clash.');
END OPEN_CLASS;
当我运行此程序时,如果记录没有任何碰撞,则显示PL/SQL procedure successfully completed.
并在表中添加记录。问题是,即使它发现冲突并且没有添加记录,它仍然会说出相同的消息,而不是我异常的输出。
答案 0 :(得分:2)
由于您在程序中捕获了异常 - 该过程不会异常终止。所以,public static class Board
{
private static Square[] gameBoard = new Square[56];
static public void SetUpBoard()
{
for (int i = 1; i == 55; i++)
{
gameBoard[i] = new Square("Ordinary Square", i);
}
gameBoard[0] = new Square("Start", 0);
gameBoard[4] = new Square.Lose_Square("Lose Square", 4);
gameBoard[5] = new Square.Chance_Square("Chance Square", 5);
gameBoard[9] = new Square.Win_Square("Win Square", 9);
gameBoard[11] = new Square.Chance_Square("Chance Square", 11);
gameBoard[14] = new Square.Lose_Square("Lose Square", 14);
gameBoard[17] = new Square.Chance_Square("Chance Square", 17);
gameBoard[19] = new Square.Win_Square("Win Square", 19);
gameBoard[24] = new Square.Lose_Square("Lose Square", 24);
gameBoard[29] = new Square.Win_Square("Win Square", 29);
gameBoard[34] = new Square.Lose_Square("Lose Square", 34);
gameBoard[35] = new Square.Chance_Square("Chance Square", 35);
gameBoard[39] = new Square.Win_Square("Win Square", 39);
gameBoard[44] = new Square.Lose_Square("Lose Square", 44);
gameBoard[47] = new Square.Chance_Square("Chance Square", 47);
gameBoard[49] = new Square.Win_Square("Win Square", 49);
gameBoard[53] = new Square.Chance_Square("Chance Square", 53);
gameBoard[55] = new Square("Finish", 56);
}
public static Square GetGameBoardSquare(int n)
{
Square S = new Square();
S = gameBoard[n];
return S;
}
public static Square StartSquare()
{
return gameBoard[0];
}
public static Square NextSquare(int n)
{
return gameBoard[(n+1)];
}
}
是正确的输出。要使您的过程抛出异常,您需要在EXCEPTION块中发出PL/SQL procedure successfully completed.
。
如果由于raise;
输出被抑制,您可能看不到此消息。如果您将DBMS_OUTPUT
作为程序的第一行,请检查是否看到任何输出。如果您没有 - 请在调用您的程序之前检查您是否在sqlplus会话中调用DBMS_OUTPUT.put_line
。
答案 1 :(得分:0)
您可能没有在会话中执行set serveroutput on
。
您的代码将引发异常,找不到数据。我认为您期望在没有匹配项时使用NULL
,因此您已使用NVL。但是select语句不会返回任何内容,而不是您期望的NULL
。
这是没有发现数据异常的原因。
您可以使用count(*) into x
和count(*) into y
代替NVL。它应该可以正常工作。