1. Introduction
In this tutorial, We'll learn what is UnknownHostException and What is the cause to produce it. And also learn how to prevent it. UnknownHostException is a common exception and will show the best ways to prevent this exception.Hierarchy:
java.lang.Object java.lang.Throwable java.lang.Exception java.io.IOException java.rmi.RemoteException java.rmi.UnknownHostException
2. When is the Exception Thrown?
UnknownHostException is thrown if and if only there was a problem with a domain name or mistake in typing. And also indicates that the IP Address of a website could not be determined.
package com.java.w3schools.blog.exceptions; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class UnknownHostExceptionException { public static void main(String[] args) throws IOException { String hostname = "http://javaprogram.comm"; URL url = null; try { url = new URL(hostname); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.getResponseCode(); } }
Output:
Exception in thread "main" java.net.UnknownHostException: javaprogram.comm at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220) at java.base/java.net.Socket.connect(Socket.java:591) at java.base/java.net.Socket.connect(Socket.java:540) at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182) at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474) at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569) at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:242) at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:341) at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:362) at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1242) at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181) at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1075) at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1009) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1581) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509) at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:527) at com.java.w3schools.blog.exceptions.UnknownHostExceptionException.main(UnknownHostExceptionException.java:21)In the above program by mistakenly, added comm instead of com. Because this, it generated error.
3. How to Prevent It?
Prevention is better than cure. Always prevention comes in the first place rather than facing the problem.
Below are important tips to prevent.
Double-check hostname - It is good to check the spelling of the domain and trim the extra spaces.
Check DNS Settings - Ensure that the DNS server is up and running using ping hostname command from your machine before running the application. If the hostname is new then wait for some time to DNS server to be reachable.
4. How to Handle?
UnknownHostException extends IOException, which is a checked exception. Similar to any other checked exception, we must either throw it or surround it with a try-catch block.
Let's handle the exception in our example:
try { con.getResponseCode(); } catch (UnknownHostException e) { con.disconnect(); }
It's a good practice to close the connection when UnknownHostException occurs. A lot of wasteful open connections can cause(memory leak) the application to run out of memory.
5. Conclusion
In this article, We've seen What is UnknownHostException and how to produce and handle it? And also seen the Best ways to prevent it.
GitHub Sample Code
API
No comments:
Post a Comment
Please do not add any spam links in the comments section.