PRAGMA EXCEPTION_INIT的用法

本文详细介绍了PL/SQL中的异常处理机制,包括如何使用PRAGMA EXCEPTION_INIT来处理未命名的内部异常,以及如何通过自定义异常和使用RAISE_APPLICATION_ERROR来创建和处理自定义错误。同时提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:http://blog.youkuaiyun.com/wanggangytsoft/article/details/5408692

PRAGMA EXCEPTION_INIT的用法

PRAGMA EXCEPTION_INIT的用法

如果要处理未命名的内部异常,必须使用OTHERS异常处理器或PRAGMA EXCEPTION_INIT 。PRAGMA由编译器控制,或者是对于编译器的注释。PRAGMA在编译时处理,而不是在运行时处理。EXCEPTION_INIT告诉编译器将异常名与ORACLE错误码结合起来,这样可以通过名字引用任意的内部异常,并且可以通过名字为异常编写一适当的异常处理器。
  
  在子程序中使用EXCEPTION_INIT的语法如下:
  PRAGMA EXCEPTION_INIT(exception_name, -Oracle_error_number);
  
  在该语法中,异常名是声明的异常,下例是其用法:
  DECLARE
  deadlock_detected EXCEPTION;
  PRAGMA EXCEPTION_INIT(deadlock_detected, -60);
  BEGIN
  ... -- Some operation that causes an ORA-00060 error
  EXCEPTION
  WHEN deadlock_detected THEN
  -- handle the error
  END;
下面看一个例子

create or replace procedure SP_Del_Test
(P_ItemAdmin in mfitem.itemadmin%type, --ItemAdmin
 P_ItemCd    in mfitem.itemcd%type, --ItemCode
 P_Return    out number --输出参数
 ) is
  exp exception;
  PRAGMA Exception_Init(exp, -2292);
begin
  delete from mfitem t
   where t.itemcd = P_ItemCd
     and t.itemadmin = P_ItemAdmin;  --这一句会引发-2292异常,有级连删除异常
EXCEPTION
  WHEN EXP THEN
    P_Return := 9;
    ROLLBACK;
  WHEN OTHERS THEN
    ROLLBACK;
end SP_Del_Test;  

使用自定义的异常,自定我们自己的错误消息:过程RAISE_APPLICATION_ERROR

调用RAISE_APPLICATION_ERROR的语法如下:

raise_application_error(error_number, message[, {TRUE | FALSE}]);

error_number是一个范围在-20000至-20999之间的负整数,message是最大长度为2048字节的字符串。如果第三个可选参数为TRUE的话,错误就会被放到前面错误的栈顶。如果为FALSE(默认值),错误就会替代前面所有的错误。

CREATE PROCEDURE raise_salary (emp_id NUMBER, amount NUMBER) AS
   curr_sal   NUMBER;
BEGIN
  SELECT sal
    INTO curr_sal
    FROM emp
   WHERE empno = emp_id;
  IF curr_sal IS NULL THEN
     /* Issue user-defined error message. */
     raise_application_error (-20101, 'Salary is missing');
  ELSE
    UPDATE emp
       SET sal = curr_sal + amount
     WHERE empno = emp_id;
  END IF;
END raise_salary;

如何抛出PL/SQL异常

DECLARE
   out_of_stock     EXCEPTION;
   number_on_hand   NUMBER (4);
BEGIN
   ...
  IF number_on_hand < 1 THEN
    RAISE out_of_stock;
  END IF;
EXCEPTION
  WHEN out_of_stock THEN
    -- handle the error
END; 



如下代码意思: """Async gunicorn worker for aiohttp.web""" import asyncio import os import re import signal import sys from types import FrameType from typing import Any, Awaitable, Callable, Optional, Union # noqa from gunicorn.config import AccessLogFormat as GunicornAccessLogFormat from gunicorn.workers import base from aiohttp import web from .helpers import set_result from .web_app import Application from .web_log import AccessLogger try: import ssl SSLContext = ssl.SSLContext except ImportError: # pragma: no cover ssl = None # type: ignore[assignment] SSLContext = object # type: ignore[misc,assignment] __all__ = ("GunicornWebWorker", "GunicornUVLoopWebWorker") class GunicornWebWorker(base.Worker): # type: ignore[misc,no-any-unimported] DEFAULT_AIOHTTP_LOG_FORMAT = AccessLogger.LOG_FORMAT DEFAULT_GUNICORN_LOG_FORMAT = GunicornAccessLogFormat.default def __init__(self, *args: Any, **kw: Any) -> None: # pragma: no cover super().__init__(*args, **kw) self._task: Optional[asyncio.Task[None]] = None self.exit_code = 0 self._notify_waiter: Optional[asyncio.Future[bool]] = None def init_process(self) -> None: # create new event_loop after fork asyncio.get_event_loop().close() self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) super().init_process() def run(self) -> None: self._task = self.loop.create_task(self._run()) try: # ignore all finalization problems self.loop.run_until_complete(self._task) except Exception: self.log.exception("Exception in gunicorn worker") self.loop.run_until_complete(self.loop.shutdown_asyncgens()) self.loop.close() sys.exit(self.exit_code) async def _run(self) -> None: runner = None if isinstance(self.wsgi, Application): app = self.wsgi elif asyncio.iscoroutinefunction(self.wsgi):
最新发布
03-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值