Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: java UnknownHostException issue

  1. #1
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    java UnknownHostException issue

    Hey guys.

    I'am running a line of code as follows
    Code:
    if(InetAddress.getByName(string).isReachable(200))
    In a case i have the hostname does not exist on the system, and can their for not be resolved to an ip address. This generates a UnknownHostException and crash my program.

    How can i avoid that the exception crash my program but just checks the next host? If one cant be resolved.

    Thanks on advance.
    Kind regards.

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: java UnknownHostException issue

    Use a try...catch?
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: java UnknownHostException issue

    Quote Originally Posted by Bachstelze View Post
    Use a try...catch?
    I have a try catch

    Code:
    while(end != true)
            {
                try 
                {
                    System.out.println("Program startet");
                    boolean created = l.createMailDOTtxt();
                    if(created == false)
                    {
                        System.out.println("Program exited, necessary files was not created.");
                        System.exit(1);
                    }
                    ArrayList<String> systemsAlive = l.checkCombiValues(WTF);
                } 
                catch(UnknownHostException ex)
                {
                    System.out.println(ex);
                }
                catch(IOException ex)
                {
                    System.out.println(ex);
                }
                catch(InterruptedException ex)
                {
                    System.out.println(ex);
                }
                end = true;
            }
    but still the program crash when the "unknownhostexception occures"....... i don't get it.
    Last edited by Drenriza; July 23rd, 2012 at 10:27 AM.

  4. #4
    Join Date
    Nov 2007
    Location
    Stockholm, Sweden
    Beans
    37
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: java UnknownHostException issue

    Code:
    if(InetAddress.getByName(string).isReachable(200))
    I can't find that code in your latest reply?

    Please provide the stacktrace (error log) and I'll try to see what the problem is.

  5. #5
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: java UnknownHostException issue

    Quote Originally Posted by mickeelm View Post
    Code:
    if(InetAddress.getByName(string).isReachable(200))
    I can't find that code in your latest reply?

    Please provide the stacktrace (error log) and I'll try to see what the problem is.
    In my main class i have the following.
    Code:
    public static void main(String[] args) 
        {
            Logic l = Logic.getInstance();
            //boolean mailSend = false;
            
            boolean end = false;
            boolean WTF = false;
            
            while(end != true)
            {
                try 
                {
                    System.out.println("Program startet");
                    boolean created = l.createMailDOTtxt();
                    if(created == false)
                    {
                        System.out.println("Program exited, necessary files was not created.");
                        System.exit(1);
                    }
                    ArrayList<String> systemsAlive = l.checkCombiValues(WTF);
                } 
                catch(UnknownHostException ex)
                {
                    System.out.println(ex);
                }
                catch(IOException ex)
                {
                    System.out.println(ex);
                }
                catch(InterruptedException ex)
                {
                    System.out.println(ex);
                }
                end = true;
            }
        }
    I then have a logic class where i have the methode
    Code:
    public ArrayList checkCombiValues(boolean WTFC) throws UnknownHostException, IOException
        {
            ArrayList<String> check = new ArrayList<String>();
            ArrayList<String> checked = new ArrayList<String>();
            check.add("vod");
            check.add("audio");
            check.add("vnc");
            byte count = 0;
            byte countTwo = 0;
            
            checked.add("localhost");//STATIC VALUE. Always need to be their.
            
            BufferedWriter bw = new BufferedWriter(new FileWriter(new File("/home/nice/mail.txt"), true));
            
            for(String string : check)
            {
                if(InetAddress.getByName(string).isReachable(200))
                {
                    String ip = InetAddress.getByName(string).getHostAddress();
                    if(ip.equals("192.168.20.1"))
                    {
                        if(count == 0 && WTFC == true)
                        {
                            bw.write("\nSystem information \n");
                        }
                        else if(count == 0 && WTFC == false)
                        {
                            bw.write("System information \n");
                        }
                        bw.write(check.get(count) + " is combi \n");
                        count++;
                    }
                    else
                    {
                        checked.add(check.get(countTwo));
                    }
                }
                countTwo++;
            }
            bw.close();
            return checked;   
        }
    The methode is a bit messy at the moment because, i'am still working with it.
    But from what i can tell the error occurs in the bold part. The inet function tries and resolve a hostname (that does not exist) to an ip address. When it cant do that it throws an UnknownHostException. The issue is. When it throws this exception the program crash. Even tho the exception is catched in the main class in a while loop.

  6. #6
    Join Date
    Nov 2007
    Location
    Stockholm, Sweden
    Beans
    37
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: java UnknownHostException issue

    If you want the program to check the next host if one fails (e.g. causes an UnknownHostException), you should have a try/catch in your for-loop, not in your main class. For the for loop to continue, you must catch the exception before the end of the current iteration.

    So remove the UnknownHostException-catch from your main class (you don't have to if you dont want, but it will be redundant).

    I haven't really examined your logic in whole but putting your catch inside the for loop in the checkCombiValues-method will give you the behavior you are looking for.

    But, if you just want the loop to go on like nothing happened, change from:

    Code:
    catch(UnknownHostException ex)
                {
                    System.out.println(ex);
                }
    to

    Code:
    catch(UnknownHostException ex)
                {
                    continue;
                }
    or something like that. Maybe you want to log the error or whatever.

    Sorry, I'm in a bit of a hurry so if this doesn't help - please reply and I'll dig into your code more when I get back tonight and solve your problem

  7. #7
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: java UnknownHostException issue

    If i have my try catch in my main methode, the program crash when the exception hits. If i put the try catch around the for loop inside the methode, the program catch the exception and continue. But does not check the last two values inside the String ArrayList........

    How can i solve this pesky issue?

  8. #8
    Join Date
    Nov 2007
    Location
    Stockholm, Sweden
    Beans
    37
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: java UnknownHostException issue

    Ok, so we're half way there. I think I'll need your code again then

    Anyways, to sum the try-catch thing up (if you didn't get it already) is that if you would have kept your try/catch in your main method, the catch would occur AFTER the checkCombiValues-method is "gone"/left by the program. Now, your catch occurs DURING the checkCombiValues-method.This is needed to get the behavior you are looking for.

    It sounds strange that the other hosts aren't checked. How does that behavior show itself? Nothing is printed in your desired files or something else?

    I'll need your code to go further. And also how you determine that the program isn't behaving as you prefer.

  9. #9
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: java UnknownHostException issue

    Quote Originally Posted by Drenriza View Post
    ... the program crash when the exception hits.
    Twice (if not more) you have used the term "crash". What do you mean by this? If Java throws an unhandled exception, surely you would have some output that indicates the problem. But you have yet to provide this critical piece of information (as was requested earlier).

    Please, rather than toy with the notion that everyone must have a crystal ball, please post all of your code and any errors that are generated when you run your application. Also, post what you think the code should do... it's your application built using your requirements.

  10. #10
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: java UnknownHostException issue

    If i'am missing something please tell me. Im still fairly new to this.

    Code:
    public class ContentServerMonitorLocal 
    {
    
        public static void main(String[] args) 
        {
            Logic l = Logic.getInstance();
            
            boolean end = false;
            boolean WTF = false;
            
            while(end != true)
            {
                try 
                {
                    System.out.println("Program startet");
                    boolean created = l.createMailDOTtxt();
                    if(created == false)
                    {
                        System.out.println("Program exited, necessary files was not created.");
                        System.exit(1);
                    }
                    ArrayList<String> systemsAlive = l.checkCombiValues(WTF);
                } 
                catch(UnknownHostException ex)
                {
                    System.out.println(ex);
                }
                catch(IOException ex)
                {
                    System.out.println(ex);
                }
                catch(InterruptedException ex)
                {
                    System.out.println(ex);
                }
                end = true;
            }
        }
    }
    The checkCombiValues() method.
    Code:
    public ArrayList checkCombiValues(boolean WTFC) throws UnknownHostException, IOException
        {
            ArrayList<String> check = new ArrayList<String>();
            ArrayList<String> checked = new ArrayList<String>();
            check.add("vod");
            check.add("audio");
            check.add("vnc");
            byte count = 0;
            byte countTwo = 0;
            
            checked.add("localhost");//STATIC VALUE. Always need to be their.
            
            BufferedWriter bw = new BufferedWriter(new FileWriter(new File("/home/nice/mail.txt"), true));
            //try
            //{
            for(String string : check)
            {
                if(InetAddress.getByName(string).isReachable(200))
                {
                    String ip = InetAddress.getByName(string).getHostAddress();
                    if(ip.equals("192.168.20.1"))
                    {
                        if(count == 0 && WTFC == true)
                        {
                            bw.write("\nSystem information \n");
                        }
                        else if(count == 0 && WTFC == false)
                        {
                            bw.write("System information \n");
                        }
                        bw.write(check.get(count) + " is combi \n");
                        count++;
                    }
                    else
                    {
                        checked.add(check.get(countTwo));
                    }
                }
                countTwo++;
            }
            /*}catch(UnknownHostException ex)
            {}*/
            bw.close();
            return checked;   
        }
    In my public static void main() method i have my l.methodes inside a while loop with a try catch. What i want is when a exception is thrown, it is catched in the main method. But the program should continue.

    In my checkCombiValues() method (from what i can tell) the problem lies in the following line
    Code:
    if(InetAddress.getByName(string).isReachable(200))
    Since it cannot resolve the hostname it throws an unknownhostexception.

    The try catch
    If i use my try catch in my main methode to catch the exception, it is printed to the screen just fine. But the program also Crash / Stops / End, however you want to put it.

    Also in my code example above. If i keep the try catch around the for loop and comment out the one in the public static void main() methode. The program does NOT Crash / Stops / End when the exception is thrown. The exception is catched and printed to the screen, afterwards the checkCombiValues() skipt the two last entries in the
    Code:
    ArrayList<String> check = new ArrayList<String>();
    , which is "audio" and "vnc"
    Code:
    check.add("audio"); 
    check.add("vnc");
    What i would like the program to do
    Is throw and catch the unknownhostexception when it occures, but the program should continue and check the remaining values in the
    Code:
    ArrayList<String> check = new ArrayList<String>();
    until all values have been checked. And then continue with the rest of the program.
    Last edited by Drenriza; July 24th, 2012 at 06:47 AM.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •