Reading:  

To the point guide to PL/SQL


Working with Exceptions

PL/SQL exception is a run time error during execution of a program. To handle exception PL/SQL supports developers using EXCEPTION block in the program. There are built in exception or user can also define exception. Built in exception are array out of bound, out of memory, division by zero.

Types of Exceptions:

  • System-defined exceptions
  • User-defined exceptions

System-defined exceptions:

Syntax:

DECLARE
   	Declaration part; 
BEGIN 
   	statements; 
EXCEPTION
	WHEN built-in_exception1 THEN
		User defined statement to handle exception;
	WHEN built-in_exception2 THEN
		User defined statement to handle exception;	
END;

Example:

DECLARE
	tmp enum%rowtype;
BEGIN
	SELECT * INTO tmp FROM enum
		WHERE id=3;
EXCEPTION
	WHEN no_data_found THEN
	dbms_output.put_line("Table doesn’t have any data");
END;
/

Result:

Table doesn’t have any data
PL/SQL procedure successfully operation.

Adding Exceptions:

In PL/SQL user can also define exception.

Declare exception:
user_define_exception_name EXCEPTION;

RAISE exception:

This statement to raised defined exception name and control transfer to an EXCEPTION block.

RAISE name _of_user_define_exception;

Exception condition implementation:

In PL/SQL WHEN condition to implement user define EXCEPTION block is added.

WHEN name_of_user_define_exception THEN
User defined statement;

Syntax:

DECLARE
	Name_of_user_define_exception_ EXCEPTION;
BEGIN
	statements;
	IF condition THEN
	RAISE name_of_user_define_exception;
	END IF;
EXCEPTION
	WHEN name_of_user_define_exception THEN
		User defined statement;	
END;
/

Example:

DECLARE
	exceptionexp EXCEPTION;
	y NUMBER;
BEGIN
	FOR y IN (SELECT * FROM enum) LOOP
		IF y.id = 2 THEN
			RAISE exceptionexp;
		END IF;
	END LOOP;
EXCEPTION
	WHEN exceptionexp THEN
	 dbms_output.put.line('This id already exist in table enum.');
END;
/

Result:

This id already exist in table enum
PL/SQL procedure successfully operation.

 

Some Pre-defined Exceptions:

Exception

Description

ACCESS_INTO_NULL

The uninitialized NULL is assigned to object then Exception is raised.

CASE_NOT_FOUND

When no choice case found in CASE statement as well as no ELSE clause in CASE statement then Exception is raised

CURSOR_ALREADY_OPEN

When user try to open a cursor that is already opened Exception raised

DUP_VAL_ON_INDEX

When user store duplicate value in unique constraint column Exception raised

INVALID_CURSOR

When user performs operation on cursor and cursor is not opened Exception raised.

INVALID_NUMBER

When user try to explicitly convert from string to a number fail Exception raised.

LOGIN_DENIED

When log in into oracle with wrong authentication Exception raised.

NO_DATA_FOUND

When SELECT ... INTO statement doesn't fetch any row from a table Exception raised.

NOT_LOGGED_ON

When program try to fetch data from table and actually connection is not established with Oracle Exception raised

PROGRAM_ERROR

When program is error prone Exception raised.

TIMEOUT_ON_RESOURCE

When ORACLE is waiting for a resource Exception raised.

TOO_MANY_ROWS

When SELECT ... INTO statement returns more than one row Exception raised

VALUE_ERROR

When arithmetic, conversion, defined size constraint error occurs Exception raised

ZERO_DIVIDE

When you program try to attempt divide by zero number Exception raise

 

 

 

 

 

Description

This tutorial focus on PL/SQL and covers the below topics

  • What is PL/SQL?
  • Environment Setup
  • Variables
  • Data Types 
  • Constants 
  • Operators
  • Conditions
  • Loops
  • Strings
  • Arrays
  • Procedures
  • Functions
  • Cursors
  • Records
  • Exceptions
  • Packages
  • Triggers
  • Collections
  • Transactions
  • Date & Time
  • Object Oriented
  • DBMS Output

If you found any error with any of the docs please let us know.

 



Learning Objectives

Learn PL/SQL from a beginners perspective, this guide can also help you if you are trying to brush up your PLSQL skills

Author: Subject Coach
Added on: 20th Apr 2015

You must be logged in as Student to ask a Question.

None just yet!