* BAEL3513

* fix
This commit is contained in:
Pazis
2020-01-24 22:54:35 +03:30
committed by maibin
parent 4418ea0865
commit d3997a959f
3 changed files with 55 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -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");
}
}