2

我有下面的代码to_date('1311313', 'yymmdd'),实际上会抛出异常invalid month。哪个可以管理为

exception
when others then
  sop('date format is wrong');

这里的问题是一切都会被抓住,我不想这样做,好像会发生一些其他错误,然后它也会传递消息date format is wrong。我也不想创建用户定义的异常。只想知道哪个异常被抛出,以便我可以在我的代码中使用,如下所示

exception
when name_of_exception then
  sop('date format is wrong');
4

2 回答 2

9

The Internally Defined Exceptions section of the Oracle Database PL/SQL Language Reference says:

An internally defined exception does not have a name unless either PL/SQL gives it one (see "Predefined Exceptions") or you give it one.

You code throws the exception ORA-01830:

SQL> select to_date('1311313', 'yymmdd') from dual
               *
ERROR at line 1:
ORA-01830: date format picture ends before converting entire input string

Since it is not one of the Predefined Exceptions, you must give it a name yourself:

declare
  ex_date_format exception;
  pragma exception_init(ex_date_format, -1830);

  v_date date;
begin
   select to_date('1311313', 'yymmdd')
     into v_date
     from dual;
exception
  when ex_date_format then
    sop('date format is wrong');
end;
/
于 2013-11-18T07:25:56.780 回答
3

DATE在尝试将字符文字转换为数据类型的值期间,至少有两种方法可以处理引发的不同异常:

  1. exception_init使用pragma定义尽可能多的异常名称并将它们与 Oracle 错误代码相关联,因为许多异常to_date()函数能够引发。
  2. to_date()使用一个when others异常处理程序为函数创建一个独立的或包的一部分、包装函数。

我个人倾向于第二个。

SQL> create or replace package util1 as
  2    function to_date1(
  3      p_char_literal in varchar2,
  4      p_date_format  in varchar2
  5     ) return date;
  6  end;
  7  /
Package created

SQL> create or replace package body util1 as
  2  
  3    function to_date1(
  4      p_char_literal in varchar2,
  5      p_date_format  in varchar2
  6     ) return date is
  7    begin -- in this situation it'll be safe to use `when others`.
  8      return to_date(p_char_literal, p_date_format);
  9    exception 
 10      when others then
 11        raise_application_error(-20001, 'Not a valid date');
 12    end;
 13  
 14  end;
 15  /
Package body created

现在,只有一个异常需要处理, -20001 Not a valid date,您的 PL/SQl 块可能如下所示:

SQL> set serveroutput on;

-- [1]  otherwise, for '1311313' the ORA-01830 exception would be raised   
SQL> declare
  2    not_a_valid_date exception;
  3    pragma exception_init(not_a_valid_date, -20001);
  4    l_res date;
  5  begin         
  6    l_res := util1.to_date1('1311313', 'yymmdd');
  7  exception
  8    when not_a_valid_date then
  9      dbms_output.put_line(sqlerrm);
  10     -- or other handler sop('date format is wrong');
  11  end;
  12 /

ORA-20001: Not a valid date

-- [2] otherwise, for '000000' the ORA-01843(not a valid month) 
--     exception would be raised
SQL> declare
  2    not_a_valid_date exception;
  3    pragma exception_init(not_a_valid_date, -20001);
  4    l_res date;
  5  begin       
  6    l_res := util1.to_date1('000000', 'yymmdd');
  7  exception
  8    when not_a_valid_date then
  9      dbms_output.put_line(sqlerrm);
  10     -- or other handler sop('date format is wrong');
  11  end;
  12  /

ORA-20001: Not a valid date
于 2013-11-18T12:33:48.297 回答