How do you declare a user-defined exception?

You can declare the User defined exceptions under the DECLARE section, with the keyword EXCEPTION.

Syntax:

EXCEPTION;

In PL/SQL, you can declare a user-defined exception using the following syntax:

DECLARE
exception_name EXCEPTION;
-- other declarations and statements
BEGIN
-- your code here
RAISE exception_name;
EXCEPTION
WHEN exception_name THEN
-- handle the exception
DBMS_OUTPUT.PUT_LINE('Custom exception handled');
END;

In this example:

  • exception_name is the name you give to your user-defined exception.
  • The RAISE statement is used to explicitly raise the exception.
  • The EXCEPTION block is where you handle the exception. You can specify different actions for different exceptions if needed.

Remember to replace 'Custom exception handled' with the appropriate code to handle the exception as per your requirements.