• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get exact exception type

 
Shantanu Deshmukh
Greenhorn
Posts: 23
Java jQuery PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to connect to mysql database using JDBC. I have applied exception handling. However simply printing exception object is amateur. I want to know exact reason of exception.
For example, if I give wrong hostname it should give me exception that Hostname was incorrect.
How to go about it ?

 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SQLException has method getErrorCode() that returns a vendor-specific error code that indicates what exactly went wrong. Combined with getMessage() and possibly getSQLState() that's all the info you have.
 
Shantanu Deshmukh
Greenhorn
Posts: 23
Java jQuery PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is the function I am going to use.

How can I use SQLException here?
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the catch block empty. Very bad idea.
Either have multiple catch blocks or use


Edit: Corrected typo. Thanks Seetharaman and Campbell
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't swallow an exception. log the exception message for debugging as in below:


@maneesh:you have beaten me nicely! there is a typo in your casting idiom
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also see http://www.coderanch.com/t/548625/JDBC/java/handle-nested-exception
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:. . . or use
. . .
And check the position of all ) carefully before proceeding.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic