try / catch not "catching" Exception

3

I'm using JPA and was trying to create an EntityManager, and was testing some expcetions treatments in a class of mine. However, I'm having a problem, that the exception is not being caught by the catch.

  public static boolean openConnection() {
    try {
        if (emf == null) {
            emf = Persistence.createEntityManagerFactory("sysbex"); //linha do erro
            em = emf.createEntityManager();
            transaction = em.getTransaction();
        }

    } catch (Throwable e) {
        System.out.println("passou aqui"); //essa linha não executa 
        return false;
    }

    return true;
}

Can anyone tell me why? ps: I already tried to change the exception type, and also nothing.

    
asked by anonymous 03.08.2014 / 07:44

1 answer

2

I think the correct one would be to put the try / catch inside the if! Maybe this is the problem, try this:

public static boolean openConnection() {
    if (emf == null)  {
        try {
            emf = Persistence.createEntityManagerFactory("sysbex"); //linha do erro
            em = emf.createEntityManager();
            transaction = em.getTransaction();
        }
   catch (Throwable e) {
        System.out.println("passou aqui"); //essa linha não executa 
        return false;
    }
}
    return true;
}
    
04.08.2014 / 02:16