I want to throw PSQLException in my Java program.

throw new PSQLException("serializable error", new PSQLState("40001"));

However, Eclipse doesn't allow compile my java program, getting an error:

Cannot instantiate the type PSQLState

I use Postgres SQL database in the program. I have postgresql-42.2.5.jar library in the project. I imported these two classes, didn't help.

import org.postgresql.util.PSQLException; 
import org.postgresql.util.PSQLState;

1 Answers

0
Mark Rotteveel On

The class org.postgresql.util.PSQLState is an enum, so you can only use the values defined in that enum. You cannot create new values. However, your code should never need to instantiate a PSQLException at all, that exception is for use of the PostgreSQL JDBC Driver, not for user code.

If you need to throw a SQLException at all (and in my opinion user code normally shouldn't need to), you should use java.sql.SQLException (or one of its subclasses in the package java.sql), or define your own subclass, for example new SQLException(String reason, String SQLState).