Govur University Logo
--> --> --> -->
...

How do you handle exceptions in PL/SQL? Provide an example.



Exception handling is an essential aspect of PL/SQL (Procedural Language/Structured Query Language) programming as it allows developers to gracefully handle runtime errors and exceptions that may occur during program execution. PL/SQL provides a robust mechanism to catch, handle, and manage exceptions using the following constructs: EXCEPTION, WHEN, RAISE, and EXCEPTION\_INIT. Here is an in-depth explanation of how exceptions are handled in PL/SQL, along with an example:

1. EXCEPTION Block:
The EXCEPTION block is used to handle exceptions in PL/SQL. It allows developers to specify the actions to be taken when a specific exception occurs. The basic syntax is as follows:

```
plsql`BEGIN
-- Statements
EXCEPTION
WHEN exception_name1 THEN
-- Handling statements for exception_name1
WHEN exception_name2 THEN
-- Handling statements for exception_name2
...
END;`
```

* The EXCEPTION block starts with the keyword EXCEPTION and ends with the keyword END.
* Within the block, one or more WHEN clauses are used to specify the exceptions to be handled.
* Each WHEN clause identifies a specific exception and provides the corresponding exception handling statements.
2. RAISE Statement:
The RAISE statement is used to explicitly raise an exception in PL/SQL. It allows developers to generate custom exceptions or re-raise predefined exceptions. The syntax is as follows:

```
plsql`RAISE exception_name;`
```

* The RAISE statement specifies the name of the exception to be raised.
* It can be used within the EXCEPTION block or other program blocks to trigger an exception.
3. EXCEPTION\_INIT Pragma:
The EXCEPTION\_INIT pragma is used to associate an exception with a user-defined error code. It allows developers to map an error code to a named exception, enabling easier identification and handling of specific errors. The syntax is as follows:

```
plsql`PRAGMA EXCEPTION_INIT(exception_name, error_code);`
```

* The PRAGMA EXCEPTION\_INIT statement associates the specified exception\_name with the specified error\_code.
* This association can be used to handle specific exceptions based on their error codes.

Now, let's consider an example that demonstrates exception handling in PL/SQL:

```
plsql`DECLARE
balance NUMBER := 1000;
withdraw_amount NUMBER := 2000;
insufficient_funds EXCEPTION;
current_balance NUMBER;
BEGIN
IF withdraw_amount > balance THEN
RAISE insufficient_funds;
ELSE
current_balance := balance - withdraw_amount;
DBMS_OUTPUT.PUT_LINE('Withdrawal successful. Current balance: ' || current_balance);
END IF;
EXCEPTION
WHEN insufficient_funds THEN
DBMS_OUTPUT.PUT_LINE('Insufficient funds to make the withdrawal.');
END;`
```
In this example, we have a variable `balance` representing the current account balance and `withdraw_amount` representing the amount to be withdrawn. If the `withdraw_amount` exceeds the `balance`, the `insufficient_funds` exception is raised using the `RAISE` statement. The exception is then caught in the EXCEPTION block, and an appropriate message is displayed. Otherwise, the withdrawal is processed, and the current balance is displayed.

Exception handling allows developers to gracefully handle errors and exceptions, ensuring the robustness and stability of PL/SQL programs. By using the EXCEPTION block, RAISE statement, and EXCEPTION\_INIT pragma effectively, developers can control the flow of execution and provide meaningful error messages or perform specific actions in response to exceptional conditions.