Exception handling in PL/SQL is a mechanism that allows you to handle errors and unexpected conditions that may occur during the execution of your program. It allows you to define specific actions to be taken when a specific exception is raised.
PL/SQL provides several pre-defined exceptions that you can use in your code, such as NO_DATA_FOUND, TOO_MANY_ROWS, and VALUE_ERROR. Additionally, you can define your own exceptions and raise them when necessary.
The basic structure of exception handling in PL/SQL is as follows:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAqxSLvhaYmSTF162Jtno-tMEHj-F0YI69J1YHaaGMU4gJKOB2LQ1gCWEz6dE1HWddZqT82WAa3vpmjbHIjBha7wlwukTxukrb5XGVy4TZhgfcb2l-Qybg9J5gEWU0g_WghMhPz2S8FPqYkxdxKWgczlZK2o_LQR--yQblF2jMq_pvQ1lqesRp2Vyc/w640-h466/programmingdoor.com_plsql23.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAqxSLvhaYmSTF162Jtno-tMEHj-F0YI69J1YHaaGMU4gJKOB2LQ1gCWEz6dE1HWddZqT82WAa3vpmjbHIjBha7wlwukTxukrb5XGVy4TZhgfcb2l-Qybg9J5gEWU0g_WghMhPz2S8FPqYkxdxKWgczlZK2o_LQR--yQblF2jMq_pvQ1lqesRp2Vyc/w640-h466/programmingdoor.com_plsql23.png)
For example, the following block of code raises an exception if no data is found when trying to retrieve an employee’s salary:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhq9ckpxN24DDmeCY6puqGn6K-hy1VHCcIA4XKoTV5GWxB3fdr1SRAjCY68jAxo6zcaElc4_fSuHY2OZIg4UYfgHFUR4YKjpKR06qORmAzzOvar7phc5L2nA91ciskEa6QO_vhLlaRSq_n8LOKklgduOFWn-PBZ35Fu9rAxg2Chnmb1dlkDto5Agh5G/w640-h422/programmingdoor.com_plsql24.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhq9ckpxN24DDmeCY6puqGn6K-hy1VHCcIA4XKoTV5GWxB3fdr1SRAjCY68jAxo6zcaElc4_fSuHY2OZIg4UYfgHFUR4YKjpKR06qORmAzzOvar7phc5L2nA91ciskEa6QO_vhLlaRSq_n8LOKklgduOFWn-PBZ35Fu9rAxg2Chnmb1dlkDto5Agh5G/w640-h422/programmingdoor.com_plsql24.png)
In this example, when the SELECT statement is executed, it will raise the NO_DATA_FOUND exception if the employee with the specified ID is not found in the employees table. The code in the EXCEPTION block will then be executed, which will display the message “Employee not found” to the user.
You can also define your own exception and raise them when a specific condition occurs. For example, you might want to raise an exception if the salary is less than a minimum value.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinN7CP2BliUBMEvJoQ362bfYlrCs72nLfY8uH0iHWjveTFwN-Je_Nuj_27ldgDu4fX2pNQ4sT05cNAeumWp8Ej_A2ZBB7xPSRXgeL_3JhMjMr6lZ8ktt9rY2llfbpx4o2I5H-e-qHp2Ql5sPCkf849rez-Oc5d2cC_3Xvfgpq-Xk3J-pgys8fEaz8O/w640-h484/programmingdoor.com_plsql25.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinN7CP2BliUBMEvJoQ362bfYlrCs72nLfY8uH0iHWjveTFwN-Je_Nuj_27ldgDu4fX2pNQ4sT05cNAeumWp8Ej_A2ZBB7xPSRXgeL_3JhMjMr6lZ8ktt9rY2llfbpx4o2I5H-e-qHp2Ql5sPCkf849rez-Oc5d2cC_3Xvfgpq-Xk3J-pgys8fEaz8O/w640-h484/programmingdoor.com_plsql25.png)
In this example, the code will raise the l_exception if the selected salary is less than the minimum salary.
You can also use the RAISE_APPLICATION_ERROR procedure to raise an exception with a specific error code and message. The error code must be between -20000 and -20999.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEird7JdKgxqky978vxbbROiztwE3-YSBMUKFws4HDco-xGbQ2nuZrMlQg77pau1-bffSDSI-w_bzu0aJqLyTqOzY321kaJIg6_Mpqk_EeqxV7te0NS5bABuR5uVD_nKyhRzhqLgCgV65pGBjFwtaQys7MyW4BbfsQo45fIAYJQWo2ae_PVSiQG7UULN/w640-h294/programmingdoor.com_plsql26.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEird7JdKgxqky978vxbbROiztwE3-YSBMUKFws4HDco-xGbQ2nuZrMlQg77pau1-bffSDSI-w_bzu0aJqLyTqOzY321kaJIg6_Mpqk_EeqxV7te0NS5bABuR5uVD_nKyhRzhqLgCgV65pGBjFwtaQys7MyW4BbfsQo45fIAYJQWo2ae_PVSiQG7UULN/w640-h294/programmingdoor.com_plsql26.png)
In summary, exception handling in PL/SQL allows you to handle errors and unexpected conditions that may occur during the execution of your program. You can use pre-defined exceptions or define your own exceptions. The basic structure of exception handling includes a BEGIN block where the code that may raise an exception is executed, followed by an EXCEPTION block where you can specify the actions to be taken when a specific exception is raised.
In PL/SQL, you can also use the WHEN OTHERS clause in the exception block to handle any exception that is not handled by any of the other WHEN clauses. This is useful when you want to catch any unexpected exception that may occur.
It is also good practice to include a general exception handler in your code, so that any unhandled exception can be logged or reported. For example, you can use the DBMS_OUTPUT.PUT_LINE function to display the error code and message, or you can insert the error information into a log table.
In addition to the exception handling, you can use the IS NULL and IS NOT NULL operators in your SQL queries to check for null values in a table column and handle them accordingly.
It’s also worth noting that you can use the EXIT statement to exit from a loop or from a subprogram immediately after an exception is raised.
It is important to be thorough and thoughtful when handling exceptions in your PL/SQL code, in order to ensure that your program can handle any potential errors and unexpected conditions, and to make it more robust.