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 ?
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.
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.
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.
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.
Your exception handling clause should look like this:
EXCEPTION
WHEN SQLSTATE 'S0001'
THEN
...
END;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With