http://www.itpub.net/thread-1499223-7-1.html
70楼
下列的哪些程序块在执行之后会显示:"Trapped!"
注意:当你试图把字符串 "13-DEC-2004 6:56 PM" 使用掩码 "DD-MON-YYYY HH:MI:SS" 转换成日期型时,Oracle 会抛出 ORA-01858 错误。
(A)
DECLARE
e1 EXCEPTION;
PRAGMA EXCEPTION_INIT (e1, -1858);
BEGIN
DBMS_OUTPUT.put_line (
TO_DATE ('13-DEC-2004 6:56 PM'
, 'DD-MON-YYYY HH:MI:SS'));
EXCEPTION
WHEN e1
THEN
DBMS_OUTPUT.put_line ('Trapped!');
END;
/
SQL> DECLARE
2 e1 EXCEPTION;
3 PRAGMA EXCEPTION_INIT (e1, -1858);
4 BEGIN
5 DBMS_OUTPUT.put_line (
6 TO_DATE ('13-DEC-2004 6:56 PM'
7 , 'DD-MON-YYYY HH:MI:SS'));
8 EXCEPTION
9 WHEN e1
10 THEN
11 DBMS_OUTPUT.put_line ('Trapped!');
12 END;
13 /
DECLARE
e1 EXCEPTION;
PRAGMA EXCEPTION_INIT (e1, -1858);
BEGIN
DBMS_OUTPUT.put_line (
TO_DATE ('13-DEC-2004 6:56 PM'
, 'DD-MON-YYYY HH:MI:SS'));
EXCEPTION
WHEN e1
THEN
DBMS_OUTPUT.put_line ('Trapped!');
END;
ORA-01843: 无效的月份
ORA-06512: 在 line 5
SQL>
(B)
DECLARE
e1 EXCEPTION;
PRAGMA EXCEPTION_INIT (e1, -1858);
BEGIN
DECLARE
e2 EXCEPTION;
PRAGMA EXCEPTION_INIT (e2, -1858);
BEGIN
DBMS_OUTPUT.put_line (
TO_DATE ('13-DEC-2004 6:56 PM'
, 'DD-MON-YYYY HH:MI:SS'));
END;
EXCEPTION
WHEN e1
THEN
DBMS_OUTPUT.put_line ('Trapped!');
END;
/
SQL> DECLARE
2 e1 EXCEPTION;
3 PRAGMA EXCEPTION_INIT (e1, -1858);
4 BEGIN
5 DECLARE
6 e2 EXCEPTION;
7 PRAGMA EXCEPTION_INIT (e2, -1858);
8 BEGIN
9 DBMS_OUTPUT.put_line (
10 TO_DATE ('13-DEC-2004 6:56 PM'
11 , 'DD-MON-YYYY HH:MI:SS'));
12 END;
13 EXCEPTION
14 WHEN e1
15 THEN
16 DBMS_OUTPUT.put_line ('Trapped!');
17 END;
18 /
DECLARE
e1 EXCEPTION;
PRAGMA EXCEPTION_INIT (e1, -1858);
BEGIN
DECLARE
e2 EXCEPTION;
PRAGMA EXCEPTION_INIT (e2, -1858);
BEGIN
DBMS_OUTPUT.put_line (
TO_DATE ('13-DEC-2004 6:56 PM'
, 'DD-MON-YYYY HH:MI:SS'));
END;
EXCEPTION
WHEN e1
THEN
DBMS_OUTPUT.put_line ('Trapped!');
END;
ORA-01843: 无效的月份
ORA-06512: 在 line 9
SQL>
(C)
DECLARE
e1 EXCEPTION;
PRAGMA EXCEPTION_INIT (e1, -1858);
BEGIN
DECLARE
e2 EXCEPTION;
PRAGMA EXCEPTION_INIT (e2, -1858);
BEGIN
DBMS_OUTPUT.put_line (
TO_DATE ('13-DEC-2004 6:56 PM'
, 'DD-MON-YYYY HH:MI:SS'));
END;
EXCEPTION
WHEN e2
THEN
DBMS_OUTPUT.put_line ('Trapped!');
END;
/
SQL> DECLARE
2 e1 EXCEPTION;
3 PRAGMA EXCEPTION_INIT (e1, -1858);
4 BEGIN
5 DECLARE
6 e2 EXCEPTION;
7 PRAGMA EXCEPTION_INIT (e2, -1858);
8 BEGIN
9 DBMS_OUTPUT.put_line (
10 TO_DATE ('13-DEC-2004 6:56 PM'
11 , 'DD-MON-YYYY HH:MI:SS'));
12 END;
13 EXCEPTION
14 WHEN e2
15 THEN
16 DBMS_OUTPUT.put_line ('Trapped!');
17 END;
18 /
DECLARE
e1 EXCEPTION;
PRAGMA EXCEPTION_INIT (e1, -1858);
BEGIN
DECLARE
e2 EXCEPTION;
PRAGMA EXCEPTION_INIT (e2, -1858);
BEGIN
DBMS_OUTPUT.put_line (
TO_DATE ('13-DEC-2004 6:56 PM'
, 'DD-MON-YYYY HH:MI:SS'));
END;
EXCEPTION
WHEN e2
THEN
DBMS_OUTPUT.put_line ('Trapped!');
END;
ORA-06550: 第 14 行, 第 9 列:
PLS-00201: 必须声明标识符 'E2'
ORA-06550: 第 0 行, 第 0 列:
PL/SQL: Compilation unit analysis terminated
SQL>
(D)
DECLARE
e1 EXCEPTION;
BEGIN
DECLARE
PRAGMA EXCEPTION_INIT (e1, -1858);
BEGIN
DBMS_OUTPUT.put_line (
TO_DATE ('13-DEC-2004 6:56 PM'
, 'DD-MON-YYYY HH:MI:SS'));
END;
EXCEPTION
WHEN e1
THEN
DBMS_OUTPUT.put_line ('Trapped!');
END;
/
SQL> DECLARE
2 e1 EXCEPTION;
3 BEGIN
4 DECLARE
5 PRAGMA EXCEPTION_INIT (e1, -1858);
6 BEGIN
7 DBMS_OUTPUT.put_line (
8 TO_DATE ('13-DEC-2004 6:56 PM'
9 , 'DD-MON-YYYY HH:MI:SS'));
10 END;
11 EXCEPTION
12 WHEN e1
13 THEN
14 DBMS_OUTPUT.put_line ('Trapped!');
15 END;
16 /
DECLARE
e1 EXCEPTION;
BEGIN
DECLARE
PRAGMA EXCEPTION_INIT (e1, -1858);
BEGIN
DBMS_OUTPUT.put_line (
TO_DATE ('13-DEC-2004 6:56 PM'
, 'DD-MON-YYYY HH:MI:SS'));
END;
EXCEPTION
WHEN e1
THEN
DBMS_OUTPUT.put_line ('Trapped!');
END;
ORA-06550: 第 2 行, 第 4 列:
PLS-00700: 'E1' 的 PRAGMA EXCEPTION_INIT 必须在同一个声明部分中的异常错误声明之后
ORA-06550: 第 5 行, 第 7 列:
PL/SQL: Item ignored
SQL>
结果有出入,设置语言为英文后重跑
SQL> alter session set nls_language='american';
Session altered
SQL>
SQL> DECLARE
2 e1 EXCEPTION;
3 PRAGMA EXCEPTION_INIT (e1, -1858);
4 BEGIN
5 DBMS_OUTPUT.put_line (
6 TO_DATE ('13-DEC-2004 6:56 PM'
7 , 'DD-MON-YYYY HH:MI:SS'));
8 EXCEPTION
9 WHEN e1
10 THEN
11 DBMS_OUTPUT.put_line ('Trapped!');
12 END;
13 /
Trapped!
PL/SQL procedure successfully completed
SQL>
SQL> alter session set nls_language='american';
Session altered
SQL>
SQL>
SQL> DECLARE
2 e1 EXCEPTION;
3 PRAGMA EXCEPTION_INIT (e1, -1858);
4 BEGIN
5 DECLARE
6 e2 EXCEPTION;
7 PRAGMA EXCEPTION_INIT (e2, -1858);
8 BEGIN
9 DBMS_OUTPUT.put_line (
10 TO_DATE ('13-DEC-2004 6:56 PM'
11 , 'DD-MON-YYYY HH:MI:SS'));
12 END;
13 EXCEPTION
14 WHEN e1
15 THEN
16 DBMS_OUTPUT.put_line ('Trapped!');
17 END;
18 /
Trapped!
PL/SQL procedure successfully completed
SQL>
SQL> alter session set nls_language='american';
Session altered
SQL>
SQL>
SQL> DECLARE
2 e1 EXCEPTION;
3 PRAGMA EXCEPTION_INIT (e1, -1858);
4 BEGIN
5 DECLARE
6 e2 EXCEPTION;
7 PRAGMA EXCEPTION_INIT (e2, -1858);
8 BEGIN
9 DBMS_OUTPUT.put_line (
10 TO_DATE ('13-DEC-2004 6:56 PM'
11 , 'DD-MON-YYYY HH:MI:SS'));
12 END;
13 EXCEPTION
14 WHEN e2
15 THEN
16 DBMS_OUTPUT.put_line ('Trapped!');
17 END;
18 /
DECLARE
e1 EXCEPTION;
PRAGMA EXCEPTION_INIT (e1, -1858);
BEGIN
DECLARE
e2 EXCEPTION;
PRAGMA EXCEPTION_INIT (e2, -1858);
BEGIN
DBMS_OUTPUT.put_line (
TO_DATE ('13-DEC-2004 6:56 PM'
, 'DD-MON-YYYY HH:MI:SS'));
END;
EXCEPTION
WHEN e2
THEN
DBMS_OUTPUT.put_line ('Trapped!');
END;
ORA-06550: line 14, column 9:
PLS-00201: identifier 'E2' must be declared
ORA-06550: line 0, column 0:
PL/SQL: Compilation unit analysis terminated
SQL>
SQL> alter session set nls_language='american';
Session altered
SQL>
SQL>
SQL> DECLARE
2 e1 EXCEPTION;
3 BEGIN
4 DECLARE
5 PRAGMA EXCEPTION_INIT (e1, -1858);
6 BEGIN
7 DBMS_OUTPUT.put_line (
8 TO_DATE ('13-DEC-2004 6:56 PM'
9 , 'DD-MON-YYYY HH:MI:SS'));
10 END;
11 EXCEPTION
12 WHEN e1
13 THEN
14 DBMS_OUTPUT.put_line ('Trapped!');
15 END;
16 /
DECLARE
e1 EXCEPTION;
BEGIN
DECLARE
PRAGMA EXCEPTION_INIT (e1, -1858);
BEGIN
DBMS_OUTPUT.put_line (
TO_DATE ('13-DEC-2004 6:56 PM'
, 'DD-MON-YYYY HH:MI:SS'));
END;
EXCEPTION
WHEN e1
THEN
DBMS_OUTPUT.put_line ('Trapped!');
END;
ORA-06550: line 2, column 4:
PLS-00700: PRAGMA EXCEPTION_INIT of E1 must follow declaration of its exception in the same declarative part
ORA-06550: line 5, column 7:
PL/SQL: Item ignored
SQL>
答案AB(设置语言为英文,则答案AB;如语言为中文,答案均不正确)
答案说明74楼
A: 正确捕获-1858异常
B: 内层也定义了-1858但是没有捕获,所以外层仍然能够捕获到
C: 内层的定义在外层是不可见的,会报PLS-00201: identifier 'E2' must be declared
D: PRAGMA EXCEPTION_INIT必须和EXCEPTION定义在同一层DECLARE中: PLS-00700: PRAGMA EXCEPTION_INIT of E1 must follow declaration of its exception in the same declarative part