One of the method in my code throws UnknownHostException exception
I first had a catch block like this:
catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Custom Message "+e.getMessage());
if(e instanceof java.net.UnknownHostException){
System.out.println("Unknown Host Ex");
}else{
System.out.println("OTHER ERROR");
}
}
I am facing a problem where that if condition never evaluates to true and hence I am not able to output that there is some host error.
You can see I have a sysout just before that which prints this :
Custom Message ; nested exception is:
java.net.UnknownHostException: abc.xyz
After that I wrote a seperate catch block to handle UnknownHostException but still is it not getting catched.
Well, apparently your
UnknownHostExceptionin wrapped in some other exception. In other words some code above catchesUnknownHostExceptionand throws:Print
e.getClass()to see what kind of exception is wrapping it. You can also try:but it’s ugly.
BTW you should avoid using
instanceofand letcatchfigure out the exception itself (but it won’t help in your case):