Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:09:55+00:00 2026-06-14T18:09:55+00:00

One of the method in my code throws UnknownHostException exception I first had a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:09:56+00:00Added an answer on June 14, 2026 at 6:09 pm

    Well, apparently your UnknownHostException in wrapped in some other exception. In other words some code above catches UnknownHostException and throws:

    throw new SomeOtherException("Custom Message", unknownHostEx);
    

    Print e.getClass() to see what kind of exception is wrapping it. You can also try:

    if(e.getCause() != null && e.getCause() instanceof UnknownHostException)
    

    but it’s ugly.

    BTW you should avoid using instanceof and let catch figure out the exception itself (but it won’t help in your case):

    catch (java.net.UnknownHostException e) {
          System.out.println("Unknown Host Ex");
    }
    catch (Exception e) {
          System.out.println("OTHER ERROR");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: s(x => x.Open()); s is a method which calls one
This is a topic in one of our code reviews, I'd like more opinions.
I start right with the code. This one invokes a method using reflection try
Say I have a simple client/server scenario with one method: // client code $client
I'm trying to use a method within the code of one windows form and
I'm having some legacy code that recieves a string[,] as one of the method
I modified one method of JQuery UI Resizable to achieve a centered resize. This
Can any one help me how to run this code... If I compile this
The following code throws an exception which I have no idea what goes wrong
Let's say in one method I have try { callme(); } catch { //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.