在pl/sql代码中调用TO_DATE时要注意的确切异常

gah*_*ggs 2 oracle plsql exception to-date

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

exception
when others then
  sop('date format is wrong');
Run Code Online (Sandbox Code Playgroud)

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

exception
when name_of_exception then
  sop('date format is wrong');
Run Code Online (Sandbox Code Playgroud)

Mar*_*lli 8

" Oracle数据库PL/SQL语言参考"的" 内部定义的异常"部分说:

内部定义的异常没有名称,除非PL/SQL给它一个(参见"预定义的异常")或者给它一个.

您的代码抛出异常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
Run Code Online (Sandbox Code Playgroud)

由于它不是预定义的例外之一,您必须自己给它起一个名字:

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;
/
Run Code Online (Sandbox Code Playgroud)