import java.util.*;
import java.lang.*;
import java.io.*;
import java.net.*;
// The main method must be in a class named "Main".
class Main {
public static void main(String[] args) {
testConnection("http//192.0.2.1"); // MalformedURLException
testConnection("http://[Log in to view URL]"); // UnknownHostException
// testConnection("http://[Log in to view URL]:80"); // NoRouteToHostException (Reserved IP for documentation)
// testConnection("http://localhost:9999"); // ConnectException (Port not commonly used)
// testConnection("http://[Log in to view URL]:8080"); // SocketTimeoutException
}
private static void testConnection(String urlString) {
System.out.println("\nTesting connection to: " + urlString);
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(1000); // 3 seconds timeout
connection.setReadTimeout(1000);
connection.connect();
System.out.println("Successful connection - Code: " + connection.getResponseCode());
} catch (MalformedURLException e) {
System.err.println("Error: Malformed URL - " + e.getMessage());
System.err.println("Exception type: " + e.getClass().getName());
System.err.println("Potential cause: The URL format is invalid or contains illegal characters.");
} catch (UnknownHostException e) {
System.err.println("Error: Unknown host - " + e.getMessage());
System.err.println("Exception type: " + e.getClass().getName());
System.err.println("Potential cause: The domain does not exist or is misspelled.");
} catch (NoRouteToHostException e) {
System.err.println("Error: No route to host - " + e.getMessage());
System.err.println("Exception type: " + e.getClass().getName());
System.err.println("Potential cause: Network routing issue, firewall blocking access, or host is down.");
} catch (ConnectException e) {
System.err.println("Error: Connection refused - " + e.getMessage());
System.err.println("Exception type: " + e.getClass().getName());
System.err.println("Potential cause: The server is not running, wrong port, or the service is not accepting connections.");
} catch (SocketTimeoutException e) {
System.err.println("Error: Timeout occurred - " + e.getMessage());
System.err.println("Exception type: " + e.getClass().getName());
System.err.println("Potential cause: Server is taking too long to respond or network is too slow.");
} catch (IOException e) {
System.err.println("Error: General I/O error - " + e.getMessage());
System.err.println("Exception type: " + e.getClass().getName());
System.err.println("Potential cause: Network connectivity issues or problem with the data transfer.");
}
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: