User defined Exception [message #343989] |
Thu, 28 August 2008 09:42 |
pointers
Messages: 451 Registered: May 2008
|
Senior Member |
|
|
Hi,
Can you suggest me how to raise a userdefined exception for an no_data_found exception..?
I have done a non predefined exception for no_data_found
DECLARE
v_ename emp.ename%TYPE;
v_character VARCHAR2 (100);
DATA EXCEPTION;
PRAGMA EXCEPTION_INIT (DATA, 100);
BEGIN
SELECT ename
INTO v_ename
FROM emp
WHERE empno = 10000;--there is no emp record with empno=10000
IF (SQL%NOTFOUND)
THEN
RAISE DATA;
END IF;
EXCEPTION
WHEN DATA
THEN
DBMS_OUTPUT.put_line ('no data found exception');
END;
Can we do the same implementation using an user defined exception
Regards,
Pointers.
|
|
|
Re: User defined Exception [message #343995 is a reply to message #343989] |
Thu, 28 August 2008 09:50 |
Olivia
Messages: 519 Registered: June 2008
|
Senior Member |
|
|
* Remove DATA EXCEPTION
* Remove PRAGMA EXCEPTION_INIT (DATA, 100)
* REMOVE RAISE exceptioon
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.put_line ('no data found exception');
END;
Regards,
Oli
|
|
|
|
|
|
|
Re: User defined Exception [message #344024 is a reply to message #343989] |
Thu, 28 August 2008 11:10 |
annagel
Messages: 220 Registered: April 2006
|
Senior Member |
|
|
From your example if you wanted to raise a custom exception your first step has to be either catching the exception thrown by the SELECT into or or selecting the data in such a way that no exception is thrown when nothing is encountered now. As your code is now the explicit call to raise the DATA exception never happens.
Look at the following example I subbed out your no data found exception for a 'dump string is not valid' (-77) exception. If you run the code you will get a no-data-found exception because the exception is raised before you ever reach your explicit raise of the DATA exception.
DECLARE
v_ename VARCHAR2 (10);
v_character VARCHAR2 (100);
DATA EXCEPTION;
PRAGMA EXCEPTION_INIT (DATA, -77);
BEGIN
SELECT dummy
INTO v_ename
FROM DUAL
WHERE dummy = 'Z'; --there is no emp record with empno=10000
IF (SQL%NOTFOUND)
THEN
RAISE DATA;
END IF;
EXCEPTION
WHEN DATA
THEN
DBMS_OUTPUT.put_line ('no data found exception');
END;
|
|
|
|
Re: User defined Exception [message #344033 is a reply to message #344028] |
Thu, 28 August 2008 11:53 |
annagel
Messages: 220 Registered: April 2006
|
Senior Member |
|
|
Michel Cadot wrote on Thu, 28 August 2008 11:37 | You don't need to initialize the exception with a value to use it.
Remove your "PRAGMA EXCEPTION_INIT" and it works as well.
|
No he is going to get a NO_DATA_FOUND from select statement, removing the pragma just changes the nature of the exception that he never gets to throw....
|
|
|
|
|
|
|