Try/catch does not "catch" to Exception

Asked

Viewed 423 times

4

I’m using JPA and was trying to create an Entitymanager, and was testing some expcetions treatments in a class of mine. However, I have a problem, that Exception is not being "caught" by 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 have tried to change the type of exception, and also nothing.

  • Is there an exception being made? If you put one System.out.println("Chegou aqui"); after the } of if (emf == null), this line is executed?

  • @carlosfigueira Yes, a stackTrace is launched on the console. If I put the SOUT after emf == null, it runs, but if put after the emf = Persistence.createEntityManagerFactory("sysbex"); he does not perform.

  • 1

    Can post the stack trace of exception?

  • And a crossbow doubt (but it already affected me). You made a clean, build and redeploy total solution?

  • Put the full stackTrace

1 answer

2


I think the right thing would be to put Try/catch inside if ! Maybe that’s the problem, try it this way:

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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.