File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes how to handle nested exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "how to handle nested exception" Watch "how to handle nested exception" New topic

how to handle nested exception

Shantanu Deshmukh

Joined: Jul 28, 2011
Posts: 23


I am trying to connect to mysql database. I am using try catch while establishing connection.
I want that I should be able to know what exception was caught, so that, I can show appropriate error message.
e.g if I pass incorrect hostname, i should show Wrong hostname provided.
One way to do is get class name of runtime exception class. but nested exception is being thrown like this..

There was some error while establishing connection to the databasecom.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

MESSAGE: localhsost

STACKTRACE: localhsost
at Method)
at com.mysql.jdbc.StandardSocketFactory.connect(
at com.mysql.jdbc.MysqlIO.<init>(
at com.mysql.jdbc.Connection.createNewIO(
at com.mysql.jdbc.Connection.<init>(
at com.mysql.jdbc.NonRegisteringDriver.connect(
at java.sql.DriverManager.getConnection(
at java.sql.DriverManager.getConnection(
at accessMySQL.accessMySQL.createConnection(
at test.main(


Last packet sent to the server was 1 ms ago.
Problem in establishing connection.

Now I would like to know that class name UnknownHostException, so I can setup a condition like

please help !!

There are no failures, only experiences
Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

By fixing this typo: localhsost

OCUP UML fundamental and ITIL foundation
Darryl Burke

Joined: May 03, 2008
Posts: 5115

Also see

luck, db
There are no new questions, but there may be new answers.
Shantanu Deshmukh

Joined: Jul 28, 2011
Posts: 23

@jan cumps,

I did that typo deliberately. to get that exception. only to find out that it is throwing nested exception. i want to find out a way to get inner exception class name
Philip Grove
Ranch Hand

Joined: Aug 18, 2009
Posts: 68

Use the getCause() method, if it returns null either the cause is non-existent or unknown. That method is actually inherited from Throwable so it should be usable for all Exceptions.
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

i am not sure whether what i'm saying is right ... localhsost

i found this in your first post ...

is the name "localhost" spelled correctly ...
sze sze chan

Joined: Jul 27, 2011
Posts: 18
are you trying to find what exception you getting after you input wrong host name ?

why don't you catch any exception then give it a invalid host name and e.tostring to print the exception name ?
for example

your code
}catch(Exception e){
System.out.println("the exception is : " + e.toString());

I agree. Here's the link:
subject: how to handle nested exception
jQuery in Action, 3rd edition