Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise and catch user defined exceptions

I use the RAISE EXCEPTION '...' USING ERRCODE='....' quite a lot in my code, as I can use the error code in my C# code. However, I would like to use it now in my plpgsql code, like this:

BEGIN
    ...
    RAISE EXCEPTION 'Something is wrong' USING ERRCODE='S0001';

EXCEPTION WHEN 'S0001' THEN
    -- Handle code S0001
END;

But that doesn't work. How can I catch and process my own thrown exceptions in plpgsql ?

like image 712
Bart Friederichs Avatar asked Feb 14 '17 13:02

Bart Friederichs


People also ask

How do you increase user-defined exceptions?

User-defined exceptions are created to force certain constraints on the values of the variables. To create a User-defined Exception, we have to create a class that implements the Exception class. We can raise(throw) these exceptions using the raise keyword.

How do you raise user-defined exceptions in Java?

User Defined Exception or custom exception is creating your own exception class and throws that exception using 'throw' keyword. This can be done by extending the class Exception. There is no need to override any of the above methods available in the Exception class, in your derived class.

Does raise catch an exception?

The raise statement without any arguments re-raises the last exception. This is useful if you need to perform some actions after catching the exception and then want to re-raise it. But if there wasn't any exception before, the raise statement raises a TypeError Exception.

What is a raise exception?

Raising an exception is a technique for interrupting the normal flow of execution in a program, signaling that some exceptional circumstance has arisen, and returning directly to an enclosing part of the program that was designated to react to that circumstance.


1 Answers

Your exception handling clause should look like this:

EXCEPTION
   WHEN SQLSTATE 'S0001'
   THEN
      ...
END;
like image 79
Laurenz Albe Avatar answered Sep 22 '22 21:09

Laurenz Albe