parent
51eb5577e3
commit
fc7fe5ac4c
@ -14,11 +14,23 @@
|
|||||||
<relativePath>../../parent-java</relativePath>
|
<relativePath>../../parent-java</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- test scoped -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj-core.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
<description> </description>
|
<description> </description>
|
||||||
<url>http://maven.apache.org</url>
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<!-- testing -->
|
||||||
|
<assertj-core.version>3.10.0</assertj-core.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
|
public class UnknownHostExceptionHandling {
|
||||||
|
|
||||||
|
public static int getResponseCode(String hostname) throws IOException {
|
||||||
|
URL url = new URL(hostname.trim());
|
||||||
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||||
|
int resCode = -1;
|
||||||
|
try {
|
||||||
|
resCode = con.getResponseCode();
|
||||||
|
} catch (UnknownHostException e){
|
||||||
|
con.disconnect();
|
||||||
|
}
|
||||||
|
return resCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getResponseCodeUnhandled(String hostname) throws IOException {
|
||||||
|
URL url = new URL(hostname.trim());
|
||||||
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||||
|
int resCode = con.getResponseCode();
|
||||||
|
return resCode;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class UnknownHostExceptionHandlingUnitTest {
|
||||||
|
|
||||||
|
@Test(expected = UnknownHostException.class)
|
||||||
|
public void givenUnknownHost_whenResolve_thenUnknownHostException() throws IOException {
|
||||||
|
UnknownHostExceptionHandling.getResponseCodeUnhandled("http://locaihost");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user