Site Name

Copyright © 2019 All rights reserved | This template is made with ♥ by Gulshan

Test whether a URL exits or not in Java26

This piece of code tries to connect with specified url , if successfully connected, proceed ahead and prints "URL exists"; otherwise, UnknownHostException is thrown and you can handle the situation in catch block as shown: try { URL url = new URL("http://www.google.com"); URLConnection urlc = url.openConnection(); urlc.connect();//<--- throws UnknownHostException when unable to connect!! System.out.println("URL exists"); } catch(UnknownHostException e) { System.out.println("URL either doesn't exist or unable to connect at this moment"); } catch(Exception e) {e.printStackTrace();}

published by gulshan on 2019-12-13 06:55:06

0 likes 0 dislikes

www.000webhost.com