Como tratar exception?

2 respostas
A

Pessoal, estou com um problema nos meus exceptions.
Quando faço uma inserção violando a UniqueKey, ele dá o seguinte erro:

Hibernate operation: could not insert: [org.tcc.model.Categoria]; uncategorized SQLException for SQL [insert into CATEGORIA (VERSION, DESCRICAO, IDCATEGORIA) values (?, ?, ?)]; SQL state [HY000]; error code [335544665]; GDS Exception. 335544665. violation of PRIMARY or UNIQUE KEY constraint "INTEG_209" on table "CATEGORIA"; nested exception is org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544665. violation of PRIMARY or UNIQUE KEY constraint "INTEG_209" on table "CATEGORIA"

O meu exception está assim:

public void salvarCategoria(Categoria categoria) throws CategoriaExistsException {
		try {
		categoriaDao.salvarCategoria(categoria);
		}
		catch (DataIntegrityViolationException e){
			throw new CategoriaExistsException("errors.existing.categoria");
		}
	}

O que devo fazer nesse método para ele tratar aquele erro?

Obrigado

2 Respostas

J

Nao sei se e bem isso que vc quer mais vamos vou dar uma exemplo bem simples:

public class Ex {
publec static int div(int a, int b ) throws Exception {
  try{  
    return  a/b;
   }catch(Exception erro){
         throw new Exception("Erro - Nao existi divisão por 0 "); 
   }
}

public static void main(String... args){
 try{
       System.out.print(Ex.div(2,0));
  }catch(Exception e){
      e.getMessage();
  }
} 
}

Esse codigo ira imprimir Erro - Nao existi divisão por 0, em vez do StackTrace java.lang.ArithmeticException: / by zero ou seja o erro foi tratado com uma mensagem mais amigavel !

espero ter ajudado !

A

“JVander”:
Nao sei se e bem isso que vc quer mais vamos vou dar uma exemplo bem simples:

Esse codigo ira imprimir Erro - Nao existi divisão por 0, em vez do StackTrace java.lang.ArithmeticException: / by zero ou seja o erro foi tratado com uma mensagem mais amigavel !

espero ter ajudado !

Olá JVander, o meu problema acho que é com o firebird, está dando uma mensagem de sqlException não especificada…

[tcc] ERROR [http-8080-Processor25] AbstractFlushingEventListener.performExecutions(300) | Could not synchronize database state with session org.hibernate.exception.GenericJDBCException: could not insert: [org.tcc.model.Categoria] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)

Criado 16 de novembro de 2006
Ultima resposta 16 de nov. de 2006
Respostas 2
Participantes 2