• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions list

 
garfild Baram
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi helper,
I have incountered a problem catching an error.
The error is a dublicate primary key that the user might want to insert into a db.
I f he tries there an error raises up and i would like to catch this specific error.
1.What is the name of the exception that catchs this error?
2.Do you know of a source that might lead me to a list of exceptions in java?
Thanks
 
Alain Boucher
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The execption raise by a duplicate key is a java.sql.SQLException... like any other problem during a SQL query. Log the exception message to know why it crash. DUplicate primary key is a programatic exception, it is not suppose to happen in a well done program, so there is no specific exception for that problem
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know of a source that might lead me to a list of exceptions in java?

Maybe the Java api (just a wild guess). API
 
garfild Baram
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick replys,
Duplicate keys is avoided but for any case I thought of creating a catcher that will catch it if... as I understood the exception does not exist.
Thanks again.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
garfild: I see your problem here. Although you know it is a SQLException, you want to distinguish between different sql exceptions and take action only on a primary key violation. This can be done, but with some portability warnings.
1. Find out what the exact error message of the SQLException.
2. have a routine that checks for this specific error message. If it matches, then you know that you have your primary key violation exception and can act accordingly.

The portability issue here is that if you change databases or even drivers, the error messages will be different. This will introduce errors into the exception handling routines.
Some other ideas are to centralize the error messages into one class. This may ease the transition to a new driver so you are not frantically checking thousands of lines of code changing all your hard coded messages to the new message.
Jamie
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic