Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to handle nested exception

 
Shantanu Deshmukh
Greenhorn
Posts: 23
Java jQuery PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
Jan Cumps
Bartender
Posts: 2584
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By fixing this typo: localhsost
 
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/548623/java/java/exact-exception-type
 
Shantanu Deshmukh
Greenhorn
Posts: 23
Java jQuery PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 68
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am not sure whether what i'm saying is right ...

java.net.UnknownHostException: localhsost


i found this in your first post ...

is the name "localhost" spelled correctly ...
 
sze sze chan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
}


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic