wood burning stoves 2.0*
The moose likes JDBC 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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to handle nested exception" Watch "how to handle nested exception" New topic
Author

how to handle nested exception

Shantanu Deshmukh
Greenhorn

Joined: Jul 28, 2011
Posts: 23

Hi,

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:

** BEGIN NESTED EXCEPTION **

java.net.UnknownHostException
MESSAGE: localhsost

STACKTRACE:

java.net.UnknownHostException: localhsost
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:867)
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1246)
at java.net.InetAddress.getAllByName0(InetAddress.java:1197)
at java.net.InetAddress.getAllByName(InetAddress.java:1128)
at java.net.InetAddress.getAllByName(InetAddress.java:1064)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:246)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:222)
at accessMySQL.accessMySQL.createConnection(accessMySQL.java:24)
at test.main(test.java:16)


** END NESTED EXCEPTION **



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
Bartender

Joined: Dec 20, 2006
Posts: 2497
    
    8

By fixing this typo: localhsost


OCUP UML fundamental and ITIL foundation
youtube channel
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

Also see http://www.coderanch.com/t/548623/java/java/exact-exception-type


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

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 ...



i found this in your first post ...

is the name "localhost" spelled correctly ...


http://plainoldjavaobject.blogspot.in
sze sze chan
Greenhorn

Joined: Jul 27, 2011
Posts: 18
hummm....
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

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


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to handle nested exception