是否可以创建用户定义的异常并能够更改sqlERRM?
例如:
DECLARE ex_custom EXCEPTION; BEGIN RAISE ex_custom; EXCEPTION WHEN ex_custom THEN DBMS_OUTPUT.PUT_LINE(sqlERRM); END; /
编辑:这里有一些更多的细节。
我希望这一个说明我想做的更好。
DECLARE l_table_status VARCHAR2(8); l_index_status VARCHAR2(8); l_table_name VARCHAR2(30) := 'TEST'; l_index_name VARCHAR2(30) := 'IDX_TEST'; ex_no_Metadata EXCEPTION; BEGIN BEGIN SELECT STATUS INTO l_table_status FROM USER_TABLES WHERE TABLE_NAME = l_table_name; EXCEPTION WHEN NO_DATA_FOUND THEN -- raise exception here with message saying -- "Table Metadata does not exist." RAISE ex_no_Metadata; END; BEGIN SELECT STATUS INTO l_index_status FROM USER_INDEXES WHERE INDEX_NAME = l_index_name; EXCEPTION WHEN NO_DATA_FOUND THEN -- raise exception here with message saying -- "Index Metadata does not exist." RAISE ex_no_Metadata; END; EXCEPTION WHEN ex_no_Metadata THEN DBMS_OUTPUT.PUT_LINE('Exception will be handled by handle_no_Metadata_exception(sqlERRM) procedure here.'); DBMS_OUTPUT.PUT_LINE(sqlERRM); END; /
实际上,有几十个这样的子块。我想知道是否有一种方法为每个子块提出一个单一的用户定义的异常,但它给一个不同的消息,而不是为每个子块创建一个单独的用户定义的异常。
在.NET中,它会像一个像这样的自定义异常:
public class ColorException : Exception { public ColorException(string message) : base(message) { } }
然后,一个方法会有这样:
if (isRed) { throw new ColorException("Red is not allowed!"); } if (isBlack) { throw new ColorException("Black is not allowed!"); } if (isBlue) { throw new ColorException("Blue is not allowed!"); }
是。您只需使用RAISE_APPLICATION_ERROR函数。如果还想命名异常,则需要使用EXCEPTION_INIT pragma命令将错误号与命名异常相关联。就像是
sql> ed Wrote file afiedt.buf 1 declare 2 ex_custom EXCEPTION; 3 PRAGMA EXCEPTION_INIT( ex_custom,-20001 ); 4 begin 5 raise_application_error( -20001,'This is a custom error' ); 6 exception 7 when ex_custom 8 then 9 dbms_output.put_line( sqlerrm ); 10* end; sql> / ORA-20001: This is a custom error PL/sql procedure successfully completed.