File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to get exact exception type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get exact exception type" Watch "How to get exact exception type" New topic
Author

How to get exact exception type

Shantanu Deshmukh
Greenhorn

Joined: Jul 28, 2011
Posts: 23

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 ?



There are no failures, only experiences
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Shantanu Deshmukh
Greenhorn

Joined: Jul 28, 2011
Posts: 23

So this is the function I am going to use.

How can I use SQLException here?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

You have the catch block empty. Very bad idea.
Either have multiple catch blocks or use


Edit: Corrected typo. Thanks Seetharaman and Campbell


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: May 03, 2008
Posts: 4523
    
    5

Also see http://www.coderanch.com/t/548625/JDBC/java/handle-nested-exception


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Maneesh Godbole wrote:. . . or use
. . .
And check the position of all ) carefully before proceeding.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to get exact exception type
 
Similar Threads
requirement of port number in url
Exception occurred while the JNDI NamingManager was processing a javax.naming.Refere
how to give port number for socket class
How to do this?
in running client for rmi-> unknown host exception:host exception need a solution