विशिष्ट exception हैंडलिंग और संदेश

PostgreSQL में Transactions और Error Handling

Jason Myers

Principal Engineer

किसी विशेष प्रकार की exception पकड़ना

DO $$
BEGIN
    UPDATE inventory SET quantity = quantity - 1 WHERE name in ('flour', 'sugar');
EXCEPTION
    WHEN check_violation THEN
           INSERT INTO errors (msg) VALUES ('Quantity can not be less than 0.');
           RAISE INFO 'Quantity can not be less than 0.';
END; 
$$ language 'plpgsql';
PostgreSQL में Transactions और Error Handling

हमारे exception हैंडलर का आउटपुट

INFO:  Quantity can not be less than 0.
DO
postgres=# select * from errors;
 error_id | state |               msg                | detail | context
^---------+-------+----------------------------------+--------+---------
        1 |       | Quantity can not be less than 0. |        |
(1 row)
PostgreSQL में Transactions और Error Handling

exception conditions के सामान्य प्रकार

Condition Name Example
unique_violation किसी unique कॉलम में एक ही मान दो बार डालना
not_null_violation ऐसे फ़ील्ड में null डालना जो null की अनुमति नहीं देता
check_violation ऐसी check constraint असफल होना, जैसे deliciousness में 11 से अधिक होना
division_by_zero 0 से division करना

So many more at the link in the citation below

1 https://www.postgresql.org/docs/9.4/errcodes-appendix.html
PostgreSQL में Transactions और Error Handling

कई exceptions पकड़ना

DO $$
BEGIN
    UPDATE inventory SET quantity = quantity - 6, cost = null 
    WHERE name='oatmeal dark chocolate';

PostgreSQL में Transactions और Error Handling

अलग-अलग exception प्रकारों को अलग से पकड़ना

-- Add check_violation exception
EXCEPTION
  WHEN check_violation THEN
     INSERT INTO errors (msg) VALUES ('Quantity can not be less than 0.');
     RAISE INFO 'Quantity can not be less than 0.';

-- Add non-null exception
  WHEN not_null_violation THEN
     INSERT INTO errors (msg) VALUES ('Cost can not be null.');
     RAISE INFO 'Cost can not be null.';
END; $$ language 'plpgsql';

PostgreSQL में Transactions और Error Handling

कई exceptions का आउटपुट पकड़ना

INFO:  Cost can not be null.
DO
postgres=# select * from errors;
 error_id | state |               msg                | detail | context
^---------+-------+----------------------------------+--------+---------
        2 |       | Cost can not be null.            |        |
(1 row)
PostgreSQL में Transactions और Error Handling

अब इसे लागू करने की बारी!

PostgreSQL में Transactions और Error Handling

Preparing Video For Download...