Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Display a variable in exception catch

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Posts
    3
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Display a variable in exception catch

    Hi,

    (new to java) I want to be notified when my box has no longer access to internet. The below test code works but it only returns a variable when I can ping a site, otherwise, it doesn't return anything.

    How can I get the "false" feedback from said code?

    TIA

    HTML Code:
    package ping;
    
    import java.io.IOException;
    import java.net.InetAddress;
    
    public class Ping {
    
        public static void main(String[] args) {
    
            InetAddress address;
            String test = "";
    
            try {
                address = InetAddress.getByName("www.google.com");
    
                boolean reachable = address.isReachable(5000);
    
                test = String.valueOf(reachable);
    
                System.out.println(test);
                
            } catch (IOException e) {
                System.out.println(test);
            }
    
        }
    }

  2. The Following User Says Thank You to ebolisa For This Useful Post:


  3. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Display a variable in exception catch

    How can I get the "false" feedback
    What is a "false" feedback?

    Where does the feedback come from?

    How do you get any "feedback" with the posted code?
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following User Says Thank You to Norm For This Useful Post:


  5. #3
    Junior Member
    Join Date
    Sep 2018
    Posts
    3
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Display a variable in exception catch

    Sorry, did not explain myself well.

    When my PC has access to internet (pinging google), the System.out.println(test) prints "true".
    When there is no connection to internet, how do I get System.out.println(test) print "false"?

  6. The Following User Says Thank You to ebolisa For This Useful Post:


  7. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Display a variable in exception catch

    how do I get System.out.println(test) print "false"?
    Set the value of test to "false"
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:


  9. #5
    Junior Member
    Join Date
    Sep 2018
    Posts
    3
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Display a variable in exception catch

    I knew it was a simple fix
    Thank you!

  10. The Following User Says Thank You to ebolisa For This Useful Post:


Similar Threads

  1. Why can't I get my variable to display?
    By geovanniluna in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 24th, 2014, 10:04 AM
  2. Throw exception in repository, catch in UI
    By Undefined in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2013, 06:44 AM
  3. Replies: 1
    Last Post: February 6th, 2013, 11:21 AM
  4. [SOLVED] Catch an Exception right
    By beruska in forum Exceptions
    Replies: 2
    Last Post: October 25th, 2011, 12:48 PM
  5. [SOLVED] Problems with Try/Catch Catching Wrong Exception
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 10th, 2011, 08:08 PM