PL/SQL User defined exceptions

The user defined exceptions are user declared exceptions in the declarative part of any PL/SQL anonymous block, subprogram or package.
In the first example exception was declared in the declarative part and then the exception is triggered inside the block using RAISE. The second example use RAISE_APPLICATION_ERROR inside the block with no need to declare exception in the declarative part.

STUDENT_ID FIRST_NAME LAST_NAME GENDER
1 Daniel SCOTT M
2 Anthony SIMMONS M
3 Sophia THOMPSON F

Raise example

declare
    v_gender varchar2(10);
    message  varchar2(250);
	my_exception exception;	
begin
    SELECT gender INTO v_gender from students WHERE student_id = 3;
    dbms_output.put_line('Gender is: '||v_gender);     
    IF v_gender = 'M' THEN 
		message := 'MALE';
	ELSE	
		RAISE my_exception;
    END IF;
    dbms_output.put_line('Message: '||message);  
exception
    when my_exception then
        dbms_output.put_line('My User defined exceptions!');
end;

Output:

Gender is: F

My User defined exceptions!

Raise_application_error example

declare
    v_gender varchar2(10);
    message  varchar2(250);
begin
    SELECT gender INTO v_gender from students WHERE student_id = 3;   
    IF v_gender = 'M' THEN 
		message := 'MALE';
	ELSE	
		raise_application_error(-20101, 'Gender is missing');
    END IF;
    dbms_output.put_line('Message: '||message);  
end;

Output:

ORA-20101: Gender is missing

ORA-06512: at line 9