I've some issues with the package I'm working on. I'm using sqldeveloper and I write in PL/SQL.
The procedure "Ajouter"
is supposed to add a swimmer, you have to specify the name, the surname, etc. When I try it and give the right informations, it works well.
But when I try to test my exceptions, it doesn't work. None of my exceptions work. For example, if I try to write "Z"
in sexe, it doesn't tell me "Sex has to be F or M"
. It only tells me "Procédure PL/SQL terminée."
, something like "PL / SQL procedure completed."
.
Where is the problem ? How can I fix this and make my exceptions work ?
Here is the "Ajouter" (Add) code :
PROCEDURE Ajouter(Nageur Nageurs%ROWTYPE) AS
ExcNull EXCEPTION;
PRAGMA EXCEPTION_INIT(ExcNull, -1400);
ExcPK EXCEPTION;
PRAGMA EXCEPTION_INIT(ExcPK, -00001);
ExcFK EXCEPTION;
PRAGMA EXCEPTION_INIT(ExcFK, -2291);
ExcCheck EXCEPTION;
PRAGMA EXCEPTION_INIT(ExcCheck, -2290);
BEGIN
INSERT INTO Nageurs values (Nageur.NRLIGUE, Nageur.NOM, Nageur.PRENOM,
Nageur.ANNEENAISS, Nageur.SEXE, Nageur.CATEGORIE, Nageur.CLUB, Nageur.ADRESSE,
Nageur.CODEPOSTAL, Nageur.LOCALITE, Nageur.NRTELEPHONE, Nageur.EMAIL, Nageur.GSM, Nageur.COTISATION);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Nageur ajouté');
EXCEPTION
WHEN ExcNull THEN
IF(INSTR(SQLERRM, 'EveNomNotNull') <> 0) THEN DBMS_OUTPUT.PUT_LINE('Le nom ne peut pas être null.');
ELSIF (INSTR(SQLERRM, 'EvePrenomNotNull') <> 0) THEN DBMS_OUTPUT.PUT_LINE('Le prenom ne peut pas être null.');
ELSIF (INSTR(SQLERRM, 'EveNaissanceNotNull') <> 0) THEN DBMS_OUTPUT.PUT_LINE('L''annee ne peut pas être null.');
ELSIF (INSTR(SQLERRM, 'EveSexeNotNull') <> 0) THEN DBMS_OUTPUT.PUT_LINE('Le sexe ne peut pas être null.');
END IF;
WHEN ExcPK THEN
IF(INSTR(SQLERRM, 'EveCpNageurs') <> 0) THEN DBMS_OUTPUT.PUT_LINE('Le nageur existe deja.');
ELSIF (INSTR(SQLERRM, 'EveUnicite') <> 0) THEN DBMS_OUTPUT.PUT_LINE('Le prénom et le nom doivent etre uniques.');
END IF;
WHEN ExcFK THEN
IF(INSTR(SQLERRM, 'EveRefNageursCat') <> 0) THEN DBMS_OUTPUT.PUT_LINE('La categorie n''existe pas');
ELSIF (INSTR(SQLERRM, 'EveRefNageursClubs') <> 0) THEN DBMS_OUTPUT.PUT_LINE('Le club n''existe pas');
ELSIF (INSTR(SQLERRM, 'EveRefNageursCP') <> 0) THEN DBMS_OUTPUT.PUT_LINE('Le code postal n''existe pas.');
END IF;
WHEN ExcCheck THEN
IF(INSTR(SQLERRM, 'EveSexe') <> 0) THEN DBMS_OUTPUT.PUT_LINE('Le sexe doit être F ou M');
ELSIF (INSTR(SQLERRM, 'EveCotisation') <> 0) THEN DBMS_OUTPUT.PUT_LINE('La cotisation doit être O ou N');
END IF;
WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE('Exception : Trop de données.');
WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Pas de données.');
WHEN OTHERS THEN RAISE;
END;
Here is the table Nageur (Swimmer) :
CREATE TABLE Nageurs
(NrLigue CHAR(14)
CONSTRAINT EveCpNageurs PRIMARY KEY,
Nom VARCHAR2(20)
CONSTRAINT EveNomNotNull NOT NULL,
Prenom VARCHAR2(20)
CONSTRAINT EvePrenomNotNull NOT NULL,
AnneeNaiss NUMBER(4)
CONSTRAINT EveNaissanceNotNull NOT NULL,
Sexe CHAR(1)
CONSTRAINT EveSexeNotNull NOT NULL
CONSTRAINT EveSexe CHECK (Sexe in ('F','M')),
Categorie CHAR(2)
CONSTRAINT EveRefNageursCat REFERENCES Categories(Categorie),Club CHAR(5)
CONSTRAINT EveRefNageursClubs REFERENCES Clubs (Club),
Adresse VARCHAR2(50),
CodePostal CHAR(5)
CONSTRAINT EveRefNageursCP REFERENCES CodePostaux (CodePostal),
Localite VARCHAR2(20),
NrTelephone CHAR(15),
EMAIL VARCHAR2(50),
GSM CHAR(15),
Cotisation CHAR(1)
CONSTRAINT EveCotisation CHECK (Cotisation in ('O','N')),
CONSTRAINT EveUnicite UNIQUE (Nom, Prenom)
);
Here an example of test that doesn't work :
DECLARE
nag nageurs%rowtype;
BEGIN
nag.nrligue := '01/000325/CCM';
nag.nom := 'DISNEY';
nag.prenom := 'WALTER';
nag.anneenaiss := '1901';
nag.sexe := 'Z';
nag.club := 'CCM';
EveGestionNageursMasters.Ajouter(nag);
END ;
When you name objects in Oracle like this, with no delimiters:
CONSTRAINT EveNomNotNull NOT NULL
The actual constraint name is non-case-sensitive, so the contraint name in the error message will be reported as:
EVENOMNOTNULL
You can check this by running a query like this:
select constraint_name from all_constraints where table_name = 'NAGEURS';
That's why the following condition using INSTR
will not detect the constraint name:
IF(INSTR(SQLERRM, 'EveNomNotNull') <> 0) THEN
Since INSTR does a case-sensitive search, you must make sure it matches the constraint name as created in the database, e.g.:
IF(INSTR(SQLERRM, 'EVENOMNOTNULL') <> 0) THEN