title: Mysql exception handling in stored procedure not working
body: <p>mysql version : mysql  Ver 8.0.20 for Linux on x86_64 (MySQL Community Server - GPL)</p>
  
  <p>My procedure:</p>
  
  <pre><code>DELIMITER //
  CREATE PROCEDURE InserirMedico(
      IN nome varchar(50),
      IN sexo char(1),
      IN nascimento date,
      IN id_horario int,
      IN vencimento int)
      BEGIN 
          DECLARE EXIT HANDLER FOR 1062 SELECT 'Duplicate keys error encountered';
          DECLARE EXIT HANDLER FOR 1318 SELECT 'Incorrect number of arguments';
          DECLARE EXIT HANDLER FOR 1048 SELECT 'Invalid null field';
          DECLARE EXIT HANDLER FOR 1452 SELECT 'One or more foreign key constraints failed';
          DECLARE EXIT HANDLER FOR 3819 SELECT 'One or more check constraints failed';
          DECLARE EXIT HANDLER FOR 1366 SELECT 'One or more columns has a incorrect data type';
  
          INSERT INTO medicos values (null, nome, sexo, nascimento, id_horario, vencimento);
      END //
  DELIMITER ;
  </code></pre>
  
  <p>My problem is that not all errors are being handled.<br>
  Error 1062,1366,1318 are not being handled and give me an error while the other ones are.<br>
  For example when i try to enter an invalid 'F' for "vencimento" field it gives me this error.</p>
  
  <pre><code>Error Code: 1366. Incorrect integer value: 'F' for column 'vencimento' at row 1
  </code></pre>
  
  <p><br><br>
  UPDATE* Provided missing info</p>
  
  <pre><code>create table horarios(
      id_horario int(3) primary key auto_increment,
      hora_inicio time not null,
      hora_fim time not null
  );
  </code></pre>
  
  <pre><code>create table medicos(
      id_med int primary key auto_increment,
      nome varchar(50) not null,
      sexo char(1) not null,
      data_nasc date not null,
      id_horario int not null,
      vencimento int not null,
      constraint fk_medicos_id_horario foreign key (id_horario) references horarios(id_horario),
      constraint CHK_medicos_ven check (vencimento &gt; 0),
      constraint CHK_medicos_sexo CHECK (sexo IN('F','M'))
  );
  </code></pre>
  
  <p>Testing the handlers</p>
  
  <pre><code>CALL InserirMedico("Mariana Venâncio", 'F', "1990:10:02",2,1000); /*Valid info*/
  CALL InserirMedico("Mariana Venâncio", 'F', "1990:10:02",2,'F'); /*Error 1366*/
  CALL InserirMedico("Mariana Venâncio", 'F', "1990:10:02",2);  /*Error 1318*/
  </code></pre>
  
  <p><br><br>
  Update2**
  For error 1062 </p>
  
  <p>I have the following table</p>
  
  <pre><code>create table tipo_pagamento(
      cod_tipo char(1) primary key,
      descricao varchar(25) unique not null,
      estado bool not null
  );
  </code></pre>
  
  <pre><code>DELIMITER //
  CREATE PROCEDURE InserirTipoDePagamento(
      IN cod_tipo char(1),
      IN descricao varchar(25),
      IN estado bool)
      BEGIN
          DECLARE EXIT HANDLER FOR 1062 SELECT 'Duplicate keys error encountered';
          DECLARE EXIT HANDLER FOR 1048 SELECT 'Invalid null field';
  
  
          INSERT INTO tipo_pagamento VALUES (cod_tipo, descricao, estado);
      END //
  DELIMITER ;
  </code></pre>
  
  <pre><code>CALL InserirTipoDePagamento('T', 'Teste', true);
  </code></pre>
  
  <p>Response:</p>
  
  <pre><code>Error Code: 1062. Duplicate entry 'T' for key 'tipo_pagamento.PRIMARY'
  </code></pre>