I want to use two exceptions for my name and age values, but it's impossible. The 100 is the code of NO_DATA_FOUND.
create or replace ....)
IS
age_excep EXCEPTION;
PRAGMA EXCEPTION_INIT(age_excep , 100);
name_excep EXCEPTION;
PRAGMA EXCEPTION_INIT(name_excep, 100);
BEGIN
.
.
.
.
EXCEPTION
WHEN name_excep THEN
DBMS_OUTPUT.PUT_LINE('Empty name : ');
WHEN age_excep THEN --- HERE IS THE ERROR.IT SAYS THAT "YOU MUST PUT name_excep AND age_excep IN SAME EXEPTION (PLS-00484)
DBMS_OUTPUT.PUT_LINE('Empty age: ');
END;
The 100 is the code of NO_DATA_FOUND.
Hmmm, that's not wrong but only if you're running in ANSI mode. By default your database will be in Oracle mode, and in that NO_DATA_FOUND hurls ORA-01403.
Anyway, the problem is you've defined two exceptions with the same error code. Oracle doesn't understand whether the error code relates to a missing name or a missing age, because that's business logic. So you can't have two exception handlers for the same error code in the same error block.
Consequently, you need to change your program.
First option (and best) is to give the exceptions numbers from the range assigned user-defined exceptions, -20999 to -20000
age_excep EXCEPTION;
PRAGMA EXCEPTION_INIT(age_excep , -20000);
name_excep EXCEPTION;
PRAGMA EXCEPTION_INIT(name_excep, -20001);
This will allow you to explicitly hurl the named exception in your code and handle it in the exception block.
EXCEPTION
WHEN name_excep THEN
DBMS_OUTPUT.PUT_LINE('Empty name : ');
WHEN age_excep THEN
DBMS_OUTPUT.PUT_LINE('Empty age: ');
END;
Whether this works for you depends on the nature of the code in the elided section you posted: how are you checking for NAME and AGE? Two different routines? SQL or PL/SQL? If you need further help with this you'll need to post some more code.
Second option is to have one exception to handle both:
age_name_excep EXCEPTION;
PRAGMA EXCEPTION_INIT(age_name_excep , -100);
You'll need a more generic error handler:
EXCEPTION
WHEN age_name_excep THEN
if name is null then
DBMS_OUTPUT.PUT_LINE('Empty name : ');
else
DBMS_OUTPUT.PUT_LINE('Empty age: ');
end if;
END;
Rather clunky I grant you, and that's why the first option is the better one.
Incidentally, "handling" exceptions by displaying a message is okay for an exercise but is considered bad practice in real life. DDBMS_OUTPUT messages can be suppressed or invisible to users in most working environments. The better approach is to log the error in a database table (or file), then re-raise the exception to the calling program. In most cases it is the prerogative of the calling program (or the end user) to decide how to handle exceptions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.