Exception Handling in Java Programming
Exception Handling in Java Programming
Oracle
Error
Explanation
DUP_VAL_ON_INDEX
TIMEOUT_ON_RESOURCE
LOGIN_DENIED
NO_DATA_FOUND
TOO_MANY_ROWS
ZERO_DIVIDE
INVALID_NUMBER
unsuccessful.
STORAGE_ERROR
PROGRAM_ERROR
VALUE_ERROR
CURSOR_ALREADY_OPEN
IS
BEGIN
INSERT INTO suppliers (supplier_id,
supplier_name )
VALUES ( supplier_id_in, supplier_name_in );
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
raise_application_error (-20001,'You have
tried to insert a duplicate supplier_id.');
WHEN OTHERS THEN
raise_application_error (-20002,'An error
has occurred inserting a supplier.');
END;
In this example, we are trapping the Named System Exception
called DUP_VAL_ON_INDEX. We are also using the WHEN
OTHERS clause to trap all remaining exceptions.
[ (parameter [,parameter]) ]
RETURN return_datatype
IS | AS
[declaration_section]
exception_name EXCEPTION;
BEGIN
executable_section
RAISE exception_name;
EXCEPTION
WHEN exception_name THEN
[statements]
WHEN OTHERS THEN
[statements]
END [function_name];
Here is an example of a procedure that uses a Named Programmer-Defined
Exception:
CREATE OR REPLACE PROCEDURE add_new_order
(order_id_in IN NUMBER, sales_in IN NUMBER)
IS
no_sales EXCEPTION;
BEGIN
IF sales_in = 0 THEN
RAISE no_sales;
ELSE
INSERT INTO orders (order_id,
total_sales )
VALUES ( order_id_in, sales_in );
END IF;
EXCEPTION
WHEN no_sales THEN
raise_application_error (-20001,'You must
have sales in order to submit the order.');
WHEN OTHERS THEN
raise_application_error (-20002,'An error
has occurred inserting an order.');
END;
In this example, we have declared a Named Programmer-Defined Exception
called no_sales in our declaration statement with the following code:
no_sales EXCEPTION;
We've then raised the exception in the executable section of the code:
IF sales_in = 0 THEN
RAISE no_sales;
Now if the sales_in variable contains a zero, our code will jump directly to
the Named Programmer-Defined Exception called no_sales.
Finally, we tell our procedure what to do when the no_sales exception is
encountered by including code in the WHEN clause:
WHEN no_sales THEN
raise_application_error (-20001,'You must
have sales in order to submit the order.');
We are also using the WHEN OTHERS clause to trap all remaining
exceptions:
WHEN OTHERS THEN
raise_application_error (-20002,'An error has
occurred inserting an order.');
00900-00999
01000-01399
01400-01499
01500-01999
02000-06499
06500-09999
10000 - 12999
Eg:
Oracle/PLSQL: ORA-00001 Error
Error Message
ORA-00001: unique constraint
(constraint_name) violated
Cause of Error
You tried to execute an INSERT or UPDATE
statement that has created a duplicate value in a
field restricted by a unique index.
Resolution
The option(s) to resolve this Oracle error are:
Option #1
Drop the unique constraint.
Option #2
Change the constraint to allow duplicate values.
Option #3
Modify your SQL so that a duplicate value is not
created.
Note
If you are not sure which unique constraint was
violated, you can run the following SQL:
select distinct table_name
from all_indexes
where index_name =
'CONSTRAINT_NAME';