Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Syntax of PL/pgSQL
Function parameters
Handling exceptions
24. Raising exceptions
Summary

Instruction

Good stuff! To raise errors/exceptions, use the RAISE EXCEPTION statement.

The example below shows how to raise an exception:

RAISE EXCEPTION 'Duplicate department_id: %', department_id;

This statement can be used anywhere in the function body. If the execution of a function encounters a RAISE EXCEPTION statement, the execution of the function is interrupted and the exception is thrown.

Exercise

Amend the template function: change the logic of the function, so that an exception is raised every time a nonexistent employee ID is passed in as an argument.

For example, when we call the function like this:

SELECT get_employees_salary(-1000);

We should receive the following message:

ERROR: Nonexistent employee ID --> -1000

Go ahead and amend the function logic so it raises an exception in the following format:

Nonexistent employee ID --> %